1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/fs.h>
20#include <linux/buffer_head.h>
21#include <linux/quotaops.h>
22#include "jfs_incore.h"
23#include "jfs_filsys.h"
24#include "jfs_metapage.h"
25#include "jfs_dinode.h"
26#include "jfs_imap.h"
27#include "jfs_dmap.h"
28#include "jfs_superblock.h"
29#include "jfs_txnmgr.h"
30#include "jfs_debug.h"
31
32#define BITSPERPAGE (PSIZE << 3)
33#define L2MEGABYTE 20
34#define MEGABYTE (1 << L2MEGABYTE)
35#define MEGABYTE32 (MEGABYTE << 5)
36
37
38#define BLKTODMAPN(b)\
39 (((b) >> 13) + ((b) >> 23) + ((b) >> 33) + 3 + 1)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62int jfs_extendfs(struct super_block *sb, s64 newLVSize, int newLogSize)
63{
64 int rc = 0;
65 struct jfs_sb_info *sbi = JFS_SBI(sb);
66 struct inode *ipbmap = sbi->ipbmap;
67 struct inode *ipbmap2;
68 struct inode *ipimap = sbi->ipimap;
69 struct jfs_log *log = sbi->log;
70 struct bmap *bmp = sbi->bmap;
71 s64 newLogAddress, newFSCKAddress;
72 int newFSCKSize;
73 s64 newMapSize = 0, mapSize;
74 s64 XAddress, XSize, nblocks, xoff, xaddr, t64;
75 s64 oldLVSize;
76 s64 newFSSize;
77 s64 VolumeSize;
78 int newNpages = 0, nPages, newPage, xlen, t32;
79 int tid;
80 int log_formatted = 0;
81 struct inode *iplist[1];
82 struct jfs_superblock *j_sb, *j_sb2;
83 uint old_agsize;
84 struct buffer_head *bh, *bh2;
85
86
87
88 if (sbi->mntflag & JFS_INLINELOG)
89 oldLVSize = addressPXD(&sbi->logpxd) + lengthPXD(&sbi->logpxd);
90 else
91 oldLVSize = addressPXD(&sbi->fsckpxd) +
92 lengthPXD(&sbi->fsckpxd);
93
94 if (oldLVSize >= newLVSize) {
95 printk(KERN_WARNING
96 "jfs_extendfs: volume hasn't grown, returning\n");
97 goto out;
98 }
99
100 VolumeSize = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
101
102 if (VolumeSize) {
103 if (newLVSize > VolumeSize) {
104 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
105 rc = -EINVAL;
106 goto out;
107 }
108 } else {
109
110 bh = sb_bread(sb, newLVSize - 1);
111 if (!bh) {
112 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
113 rc = -EINVAL;
114 goto out;
115 }
116 bforget(bh);
117 }
118
119
120
121 if (isReadOnly(ipbmap)) {
122 printk(KERN_WARNING "jfs_extendfs: read-only file system\n");
123 rc = -EROFS;
124 goto out;
125 }
126
127
128
129
130
131
132
133
134
135
136
137 if ((sbi->mntflag & JFS_INLINELOG)) {
138 if (newLogSize == 0) {
139
140
141
142
143 newLogSize = newLVSize >> 8;
144 t32 = (1 << (20 - sbi->l2bsize)) - 1;
145 newLogSize = (newLogSize + t32) & ~t32;
146 newLogSize =
147 min(newLogSize, MEGABYTE32 >> sbi->l2bsize);
148 } else {
149
150
151
152
153
154
155 newLogSize = (newLogSize * MEGABYTE) >> sbi->l2bsize;
156 }
157
158 } else
159 newLogSize = 0;
160
161 newLogAddress = newLVSize - newLogSize;
162
163
164
165
166
167
168
169
170
171
172
173 t64 = ((newLVSize - newLogSize + BPERDMAP - 1) >> L2BPERDMAP)
174 << L2BPERDMAP;
175 t32 = DIV_ROUND_UP(t64, BITSPERPAGE) + 1 + 50;
176 newFSCKSize = t32 << sbi->l2nbperpage;
177 newFSCKAddress = newLogAddress - newFSCKSize;
178
179
180
181
182 newFSSize = newLVSize - newLogSize - newFSCKSize;
183
184
185 if (newFSSize < bmp->db_mapsize) {
186 rc = -EINVAL;
187 goto out;
188 }
189
190
191
192
193
194
195 if ((sbi->mntflag & JFS_INLINELOG) && (newLogAddress > oldLVSize)) {
196 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
197 goto out;
198 log_formatted = 1;
199 }
200
201
202
203
204
205
206
207
208
209
210 txQuiesce(sb);
211
212
213 sbi->direct_inode->i_size = sb->s_bdev->bd_inode->i_size;
214
215 if (sbi->mntflag & JFS_INLINELOG) {
216
217
218
219 lmLogShutdown(log);
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 if ((rc = readSuper(sb, &bh)))
237 goto error_out;
238 j_sb = (struct jfs_superblock *)bh->b_data;
239
240
241 j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
242 j_sb->s_xsize = cpu_to_le64(newFSSize);
243 PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
244 PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
245 PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
246 PXDlength(&j_sb->s_xlogpxd, newLogSize);
247
248
249 mark_buffer_dirty(bh);
250 sync_dirty_buffer(bh);
251 brelse(bh);
252
253
254
255
256
257
258
259 if (!log_formatted)
260 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
261 goto error_out;
262
263
264
265
266 log->base = newLogAddress;
267 log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
268 if ((rc = lmLogInit(log)))
269 goto error_out;
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 newMapSize = newFSSize;
296
297
298
299
300 t64 = (newMapSize - 1) + BPERDMAP;
301 newNpages = BLKTODMAPN(t64) + 1;
302
303
304
305
306
307
308
309
310 extendBmap:
311
312 mapSize = bmp->db_mapsize;
313 XAddress = mapSize;
314 XSize = newMapSize - mapSize;
315 old_agsize = bmp->db_agsize;
316
317
318 t64 = dbMapFileSizeToMapSize(ipbmap);
319 if (mapSize > t64) {
320 printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
321 (long long) mapSize, (long long) t64);
322 rc = -EIO;
323 goto error_out;
324 }
325 nblocks = min(t64 - mapSize, XSize);
326
327
328
329
330
331
332
333
334 if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
335 goto error_out;
336
337
338
339
340
341 XSize -= nblocks;
342
343
344
345
346
347
348
349
350
351 nPages = ipbmap->i_size >> L2PSIZE;
352
353
354 if (nPages == newNpages)
355 goto finalizeBmap;
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 filemap_fdatawait(ipbmap->i_mapping);
379 filemap_write_and_wait(ipbmap->i_mapping);
380 diWriteSpecial(ipbmap, 0);
381
382 newPage = nPages;
383 xoff = newPage << sbi->l2nbperpage;
384 xlen = (newNpages - nPages) << sbi->l2nbperpage;
385 xlen = min(xlen, (int) nblocks) & ~(sbi->nbperpage - 1);
386 xaddr = XAddress;
387
388 tid = txBegin(sb, COMMIT_FORCE);
389
390 if ((rc = xtAppend(tid, ipbmap, 0, xoff, nblocks, &xlen, &xaddr, 0))) {
391 txEnd(tid);
392 goto error_out;
393 }
394
395 ipbmap->i_size += xlen << sbi->l2bsize;
396 inode_add_bytes(ipbmap, xlen << sbi->l2bsize);
397
398 iplist[0] = ipbmap;
399 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
400
401 txEnd(tid);
402
403 if (rc)
404 goto error_out;
405
406
407
408
409
410
411
412
413
414
415
416
417 if (XSize)
418 goto extendBmap;
419
420 finalizeBmap:
421
422 dbFinalizeBmap(ipbmap);
423
424
425
426
427
428
429
430
431
432
433
434
435 if (bmp->db_agsize != old_agsize) {
436 if ((rc = diExtendFS(ipimap, ipbmap)))
437 goto error_out;
438
439
440 if ((rc = diSync(ipimap)))
441 goto error_out;
442 }
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 if ((rc = dbSync(ipbmap)))
464 goto error_out;
465
466
467
468
469
470 ipbmap2 = diReadSpecial(sb, BMAP_I, 1);
471 if (ipbmap2 == NULL) {
472 printk(KERN_ERR "jfs_extendfs: diReadSpecial(bmap) failed\n");
473 goto error_out;
474 }
475 memcpy(&JFS_IP(ipbmap2)->i_xtroot, &JFS_IP(ipbmap)->i_xtroot, 288);
476 ipbmap2->i_size = ipbmap->i_size;
477 ipbmap2->i_blocks = ipbmap->i_blocks;
478
479 diWriteSpecial(ipbmap2, 1);
480 diFreeSpecial(ipbmap2);
481
482
483
484
485 if ((rc = readSuper(sb, &bh)))
486 goto error_out;
487 j_sb = (struct jfs_superblock *)bh->b_data;
488
489
490 j_sb->s_state &= cpu_to_le32(~FM_EXTENDFS);
491 j_sb->s_size = cpu_to_le64(bmp->db_mapsize <<
492 le16_to_cpu(j_sb->s_l2bfactor));
493 j_sb->s_agsize = cpu_to_le32(bmp->db_agsize);
494
495
496 if (sbi->mntflag & JFS_INLINELOG) {
497 PXDaddress(&(j_sb->s_logpxd), newLogAddress);
498 PXDlength(&(j_sb->s_logpxd), newLogSize);
499 }
500
501
502 j_sb->s_logserial = cpu_to_le32(log->serial);
503
504
505 PXDaddress(&(j_sb->s_fsckpxd), newFSCKAddress);
506 PXDlength(&(j_sb->s_fsckpxd), newFSCKSize);
507 j_sb->s_fscklog = 1;
508
509
510
511 bh2 = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
512 if (bh2) {
513 j_sb2 = (struct jfs_superblock *)bh2->b_data;
514 memcpy(j_sb2, j_sb, sizeof (struct jfs_superblock));
515
516 mark_buffer_dirty(bh);
517 sync_dirty_buffer(bh2);
518 brelse(bh2);
519 }
520
521
522 mark_buffer_dirty(bh);
523 sync_dirty_buffer(bh);
524 brelse(bh);
525
526 goto resume;
527
528 error_out:
529 jfs_error(sb, "jfs_extendfs");
530
531 resume:
532
533
534
535 txResume(sb);
536
537 out:
538 return rc;
539}
540