inode.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * linux/fs/ext2/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@dcs.ed.ac.uk), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/smp_lock.h>
  25. #include <linux/time.h>
  26. #include <linux/highuid.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/module.h>
  30. #include <linux/writeback.h>
  31. #include <linux/buffer_head.h>
  32. #include <linux/mpage.h>
  33. #include <linux/fiemap.h>
  34. #include <linux/namei.h>
  35. #include "ext2.h"
  36. #include "acl.h"
  37. #include "xip.h"
  38. MODULE_AUTHOR("Remy Card and others");
  39. MODULE_DESCRIPTION("Second Extended Filesystem");
  40. MODULE_LICENSE("GPL");
  41. static int __ext2_write_inode(struct inode *inode, int do_sync);
  42. /*
  43. * Test whether an inode is a fast symlink.
  44. */
  45. static inline int ext2_inode_is_fast_symlink(struct inode *inode)
  46. {
  47. int ea_blocks = EXT2_I(inode)->i_file_acl ?
  48. (inode->i_sb->s_blocksize >> 9) : 0;
  49. return (S_ISLNK(inode->i_mode) &&
  50. inode->i_blocks - ea_blocks == 0);
  51. }
  52. /*
  53. * Called at the last iput() if i_nlink is zero.
  54. */
  55. void ext2_delete_inode (struct inode * inode)
  56. {
  57. truncate_inode_pages(&inode->i_data, 0);
  58. if (is_bad_inode(inode))
  59. goto no_delete;
  60. EXT2_I(inode)->i_dtime = get_seconds();
  61. mark_inode_dirty(inode);
  62. __ext2_write_inode(inode, inode_needs_sync(inode));
  63. inode->i_size = 0;
  64. if (inode->i_blocks)
  65. ext2_truncate (inode);
  66. ext2_free_inode (inode);
  67. return;
  68. no_delete:
  69. clear_inode(inode); /* We must guarantee clearing of inode... */
  70. }
  71. typedef struct {
  72. __le32 *p;
  73. __le32 key;
  74. struct buffer_head *bh;
  75. } Indirect;
  76. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  77. {
  78. p->key = *(p->p = v);
  79. p->bh = bh;
  80. }
  81. static inline int verify_chain(Indirect *from, Indirect *to)
  82. {
  83. while (from <= to && from->key == *from->p)
  84. from++;
  85. return (from > to);
  86. }
  87. /**
  88. * ext2_block_to_path - parse the block number into array of offsets
  89. * @inode: inode in question (we are only interested in its superblock)
  90. * @i_block: block number to be parsed
  91. * @offsets: array to store the offsets in
  92. * @boundary: set this non-zero if the referred-to block is likely to be
  93. * followed (on disk) by an indirect block.
  94. * To store the locations of file's data ext2 uses a data structure common
  95. * for UNIX filesystems - tree of pointers anchored in the inode, with
  96. * data blocks at leaves and indirect blocks in intermediate nodes.
  97. * This function translates the block number into path in that tree -
  98. * return value is the path length and @offsets[n] is the offset of
  99. * pointer to (n+1)th node in the nth one. If @block is out of range
  100. * (negative or too large) warning is printed and zero returned.
  101. *
  102. * Note: function doesn't find node addresses, so no IO is needed. All
  103. * we need to know is the capacity of indirect blocks (taken from the
  104. * inode->i_sb).
  105. */
  106. /*
  107. * Portability note: the last comparison (check that we fit into triple
  108. * indirect block) is spelled differently, because otherwise on an
  109. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  110. * if our filesystem had 8Kb blocks. We might use long long, but that would
  111. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  112. * i_block would have to be negative in the very beginning, so we would not
  113. * get there at all.
  114. */
  115. static int ext2_block_to_path(struct inode *inode,
  116. long i_block, int offsets[4], int *boundary)
  117. {
  118. int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  119. int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
  120. const long direct_blocks = EXT2_NDIR_BLOCKS,
  121. indirect_blocks = ptrs,
  122. double_blocks = (1 << (ptrs_bits * 2));
  123. int n = 0;
  124. int final = 0;
  125. if (i_block < 0) {
  126. ext2_msg(inode->i_sb, KERN_WARNING,
  127. "warning: %s: block < 0", __func__);
  128. } else if (i_block < direct_blocks) {
  129. offsets[n++] = i_block;
  130. final = direct_blocks;
  131. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  132. offsets[n++] = EXT2_IND_BLOCK;
  133. offsets[n++] = i_block;
  134. final = ptrs;
  135. } else if ((i_block -= indirect_blocks) < double_blocks) {
  136. offsets[n++] = EXT2_DIND_BLOCK;
  137. offsets[n++] = i_block >> ptrs_bits;
  138. offsets[n++] = i_block & (ptrs - 1);
  139. final = ptrs;
  140. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  141. offsets[n++] = EXT2_TIND_BLOCK;
  142. offsets[n++] = i_block >> (ptrs_bits * 2);
  143. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  144. offsets[n++] = i_block & (ptrs - 1);
  145. final = ptrs;
  146. } else {
  147. ext2_msg(inode->i_sb, KERN_WARNING,
  148. "warning: %s: block is too big", __func__);
  149. }
  150. if (boundary)
  151. *boundary = final - 1 - (i_block & (ptrs - 1));
  152. return n;
  153. }
  154. /**
  155. * ext2_get_branch - read the chain of indirect blocks leading to data
  156. * @inode: inode in question
  157. * @depth: depth of the chain (1 - direct pointer, etc.)
  158. * @offsets: offsets of pointers in inode/indirect blocks
  159. * @chain: place to store the result
  160. * @err: here we store the error value
  161. *
  162. * Function fills the array of triples <key, p, bh> and returns %NULL
  163. * if everything went OK or the pointer to the last filled triple
  164. * (incomplete one) otherwise. Upon the return chain[i].key contains
  165. * the number of (i+1)-th block in the chain (as it is stored in memory,
  166. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  167. * number (it points into struct inode for i==0 and into the bh->b_data
  168. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  169. * block for i>0 and NULL for i==0. In other words, it holds the block
  170. * numbers of the chain, addresses they were taken from (and where we can
  171. * verify that chain did not change) and buffer_heads hosting these
  172. * numbers.
  173. *
  174. * Function stops when it stumbles upon zero pointer (absent block)
  175. * (pointer to last triple returned, *@err == 0)
  176. * or when it gets an IO error reading an indirect block
  177. * (ditto, *@err == -EIO)
  178. * or when it notices that chain had been changed while it was reading
  179. * (ditto, *@err == -EAGAIN)
  180. * or when it reads all @depth-1 indirect blocks successfully and finds
  181. * the whole chain, all way to the data (returns %NULL, *err == 0).
  182. */
  183. static Indirect *ext2_get_branch(struct inode *inode,
  184. int depth,
  185. int *offsets,
  186. Indirect chain[4],
  187. int *err)
  188. {
  189. struct super_block *sb = inode->i_sb;
  190. Indirect *p = chain;
  191. struct buffer_head *bh;
  192. *err = 0;
  193. /* i_data is not going away, no lock needed */
  194. add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
  195. if (!p->key)
  196. goto no_block;
  197. while (--depth) {
  198. bh = sb_bread(sb, le32_to_cpu(p->key));
  199. if (!bh)
  200. goto failure;
  201. read_lock(&EXT2_I(inode)->i_meta_lock);
  202. if (!verify_chain(chain, p))
  203. goto changed;
  204. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  205. read_unlock(&EXT2_I(inode)->i_meta_lock);
  206. if (!p->key)
  207. goto no_block;
  208. }
  209. return NULL;
  210. changed:
  211. read_unlock(&EXT2_I(inode)->i_meta_lock);
  212. brelse(bh);
  213. *err = -EAGAIN;
  214. goto no_block;
  215. failure:
  216. *err = -EIO;
  217. no_block:
  218. return p;
  219. }
  220. /**
  221. * ext2_find_near - find a place for allocation with sufficient locality
  222. * @inode: owner
  223. * @ind: descriptor of indirect block.
  224. *
  225. * This function returns the preferred place for block allocation.
  226. * It is used when heuristic for sequential allocation fails.
  227. * Rules are:
  228. * + if there is a block to the left of our position - allocate near it.
  229. * + if pointer will live in indirect block - allocate near that block.
  230. * + if pointer will live in inode - allocate in the same cylinder group.
  231. *
  232. * In the latter case we colour the starting block by the callers PID to
  233. * prevent it from clashing with concurrent allocations for a different inode
  234. * in the same block group. The PID is used here so that functionally related
  235. * files will be close-by on-disk.
  236. *
  237. * Caller must make sure that @ind is valid and will stay that way.
  238. */
  239. static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
  240. {
  241. struct ext2_inode_info *ei = EXT2_I(inode);
  242. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  243. __le32 *p;
  244. ext2_fsblk_t bg_start;
  245. ext2_fsblk_t colour;
  246. /* Try to find previous block */
  247. for (p = ind->p - 1; p >= start; p--)
  248. if (*p)
  249. return le32_to_cpu(*p);
  250. /* No such thing, so let's try location of indirect block */
  251. if (ind->bh)
  252. return ind->bh->b_blocknr;
  253. /*
  254. * It is going to be refered from inode itself? OK, just put it into
  255. * the same cylinder group then.
  256. */
  257. bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
  258. colour = (current->pid % 16) *
  259. (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  260. return bg_start + colour;
  261. }
  262. /**
  263. * ext2_find_goal - find a preferred place for allocation.
  264. * @inode: owner
  265. * @block: block we want
  266. * @partial: pointer to the last triple within a chain
  267. *
  268. * Returns preferred place for a block (the goal).
  269. */
  270. static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
  271. Indirect *partial)
  272. {
  273. struct ext2_block_alloc_info *block_i;
  274. block_i = EXT2_I(inode)->i_block_alloc_info;
  275. /*
  276. * try the heuristic for sequential allocation,
  277. * failing that at least try to get decent locality.
  278. */
  279. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  280. && (block_i->last_alloc_physical_block != 0)) {
  281. return block_i->last_alloc_physical_block + 1;
  282. }
  283. return ext2_find_near(inode, partial);
  284. }
  285. /**
  286. * ext2_blks_to_allocate: Look up the block map and count the number
  287. * of direct blocks need to be allocated for the given branch.
  288. *
  289. * @branch: chain of indirect blocks
  290. * @k: number of blocks need for indirect blocks
  291. * @blks: number of data blocks to be mapped.
  292. * @blocks_to_boundary: the offset in the indirect block
  293. *
  294. * return the total number of blocks to be allocate, including the
  295. * direct and indirect blocks.
  296. */
  297. static int
  298. ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
  299. int blocks_to_boundary)
  300. {
  301. unsigned long count = 0;
  302. /*
  303. * Simple case, [t,d]Indirect block(s) has not allocated yet
  304. * then it's clear blocks on that path have not allocated
  305. */
  306. if (k > 0) {
  307. /* right now don't hanel cross boundary allocation */
  308. if (blks < blocks_to_boundary + 1)
  309. count += blks;
  310. else
  311. count += blocks_to_boundary + 1;
  312. return count;
  313. }
  314. count++;
  315. while (count < blks && count <= blocks_to_boundary
  316. && le32_to_cpu(*(branch[0].p + count)) == 0) {
  317. count++;
  318. }
  319. return count;
  320. }
  321. /**
  322. * ext2_alloc_blocks: multiple allocate blocks needed for a branch
  323. * @indirect_blks: the number of blocks need to allocate for indirect
  324. * blocks
  325. *
  326. * @new_blocks: on return it will store the new block numbers for
  327. * the indirect blocks(if needed) and the first direct block,
  328. * @blks: on return it will store the total number of allocated
  329. * direct blocks
  330. */
  331. static int ext2_alloc_blocks(struct inode *inode,
  332. ext2_fsblk_t goal, int indirect_blks, int blks,
  333. ext2_fsblk_t new_blocks[4], int *err)
  334. {
  335. int target, i;
  336. unsigned long count = 0;
  337. int index = 0;
  338. ext2_fsblk_t current_block = 0;
  339. int ret = 0;
  340. /*
  341. * Here we try to allocate the requested multiple blocks at once,
  342. * on a best-effort basis.
  343. * To build a branch, we should allocate blocks for
  344. * the indirect blocks(if not allocated yet), and at least
  345. * the first direct block of this branch. That's the
  346. * minimum number of blocks need to allocate(required)
  347. */
  348. target = blks + indirect_blks;
  349. while (1) {
  350. count = target;
  351. /* allocating blocks for indirect blocks and direct blocks */
  352. current_block = ext2_new_blocks(inode,goal,&count,err);
  353. if (*err)
  354. goto failed_out;
  355. target -= count;
  356. /* allocate blocks for indirect blocks */
  357. while (index < indirect_blks && count) {
  358. new_blocks[index++] = current_block++;
  359. count--;
  360. }
  361. if (count > 0)
  362. break;
  363. }
  364. /* save the new block number for the first direct block */
  365. new_blocks[index] = current_block;
  366. /* total number of blocks allocated for direct blocks */
  367. ret = count;
  368. *err = 0;
  369. return ret;
  370. failed_out:
  371. for (i = 0; i <index; i++)
  372. ext2_free_blocks(inode, new_blocks[i], 1);
  373. return ret;
  374. }
  375. /**
  376. * ext2_alloc_branch - allocate and set up a chain of blocks.
  377. * @inode: owner
  378. * @num: depth of the chain (number of blocks to allocate)
  379. * @offsets: offsets (in the blocks) to store the pointers to next.
  380. * @branch: place to store the chain in.
  381. *
  382. * This function allocates @num blocks, zeroes out all but the last one,
  383. * links them into chain and (if we are synchronous) writes them to disk.
  384. * In other words, it prepares a branch that can be spliced onto the
  385. * inode. It stores the information about that chain in the branch[], in
  386. * the same format as ext2_get_branch() would do. We are calling it after
  387. * we had read the existing part of chain and partial points to the last
  388. * triple of that (one with zero ->key). Upon the exit we have the same
  389. * picture as after the successful ext2_get_block(), excpet that in one
  390. * place chain is disconnected - *branch->p is still zero (we did not
  391. * set the last link), but branch->key contains the number that should
  392. * be placed into *branch->p to fill that gap.
  393. *
  394. * If allocation fails we free all blocks we've allocated (and forget
  395. * their buffer_heads) and return the error value the from failed
  396. * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  397. * as described above and return 0.
  398. */
  399. static int ext2_alloc_branch(struct inode *inode,
  400. int indirect_blks, int *blks, ext2_fsblk_t goal,
  401. int *offsets, Indirect *branch)
  402. {
  403. int blocksize = inode->i_sb->s_blocksize;
  404. int i, n = 0;
  405. int err = 0;
  406. struct buffer_head *bh;
  407. int num;
  408. ext2_fsblk_t new_blocks[4];
  409. ext2_fsblk_t current_block;
  410. num = ext2_alloc_blocks(inode, goal, indirect_blks,
  411. *blks, new_blocks, &err);
  412. if (err)
  413. return err;
  414. branch[0].key = cpu_to_le32(new_blocks[0]);
  415. /*
  416. * metadata blocks and data blocks are allocated.
  417. */
  418. for (n = 1; n <= indirect_blks; n++) {
  419. /*
  420. * Get buffer_head for parent block, zero it out
  421. * and set the pointer to new one, then send
  422. * parent to disk.
  423. */
  424. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  425. branch[n].bh = bh;
  426. lock_buffer(bh);
  427. memset(bh->b_data, 0, blocksize);
  428. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  429. branch[n].key = cpu_to_le32(new_blocks[n]);
  430. *branch[n].p = branch[n].key;
  431. if ( n == indirect_blks) {
  432. current_block = new_blocks[n];
  433. /*
  434. * End of chain, update the last new metablock of
  435. * the chain to point to the new allocated
  436. * data blocks numbers
  437. */
  438. for (i=1; i < num; i++)
  439. *(branch[n].p + i) = cpu_to_le32(++current_block);
  440. }
  441. set_buffer_uptodate(bh);
  442. unlock_buffer(bh);
  443. mark_buffer_dirty_inode(bh, inode);
  444. /* We used to sync bh here if IS_SYNC(inode).
  445. * But we now rely upon generic_write_sync()
  446. * and b_inode_buffers. But not for directories.
  447. */
  448. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  449. sync_dirty_buffer(bh);
  450. }
  451. *blks = num;
  452. return err;
  453. }
  454. /**
  455. * ext2_splice_branch - splice the allocated branch onto inode.
  456. * @inode: owner
  457. * @block: (logical) number of block we are adding
  458. * @where: location of missing link
  459. * @num: number of indirect blocks we are adding
  460. * @blks: number of direct blocks we are adding
  461. *
  462. * This function fills the missing link and does all housekeeping needed in
  463. * inode (->i_blocks, etc.). In case of success we end up with the full
  464. * chain to new block and return 0.
  465. */
  466. static void ext2_splice_branch(struct inode *inode,
  467. long block, Indirect *where, int num, int blks)
  468. {
  469. int i;
  470. struct ext2_block_alloc_info *block_i;
  471. ext2_fsblk_t current_block;
  472. block_i = EXT2_I(inode)->i_block_alloc_info;
  473. /* XXX LOCKING probably should have i_meta_lock ?*/
  474. /* That's it */
  475. *where->p = where->key;
  476. /*
  477. * Update the host buffer_head or inode to point to more just allocated
  478. * direct blocks blocks
  479. */
  480. if (num == 0 && blks > 1) {
  481. current_block = le32_to_cpu(where->key) + 1;
  482. for (i = 1; i < blks; i++)
  483. *(where->p + i ) = cpu_to_le32(current_block++);
  484. }
  485. /*
  486. * update the most recently allocated logical & physical block
  487. * in i_block_alloc_info, to assist find the proper goal block for next
  488. * allocation
  489. */
  490. if (block_i) {
  491. block_i->last_alloc_logical_block = block + blks - 1;
  492. block_i->last_alloc_physical_block =
  493. le32_to_cpu(where[num].key) + blks - 1;
  494. }
  495. /* We are done with atomic stuff, now do the rest of housekeeping */
  496. /* had we spliced it onto indirect block? */
  497. if (where->bh)
  498. mark_buffer_dirty_inode(where->bh, inode);
  499. inode->i_ctime = CURRENT_TIME_SEC;
  500. mark_inode_dirty(inode);
  501. }
  502. /*
  503. * Allocation strategy is simple: if we have to allocate something, we will
  504. * have to go the whole way to leaf. So let's do it before attaching anything
  505. * to tree, set linkage between the newborn blocks, write them if sync is
  506. * required, recheck the path, free and repeat if check fails, otherwise
  507. * set the last missing link (that will protect us from any truncate-generated
  508. * removals - all blocks on the path are immune now) and possibly force the
  509. * write on the parent block.
  510. * That has a nice additional property: no special recovery from the failed
  511. * allocations is needed - we simply release blocks and do not touch anything
  512. * reachable from inode.
  513. *
  514. * `handle' can be NULL if create == 0.
  515. *
  516. * return > 0, # of blocks mapped or allocated.
  517. * return = 0, if plain lookup failed.
  518. * return < 0, error case.
  519. */
  520. static int ext2_get_blocks(struct inode *inode,
  521. sector_t iblock, unsigned long maxblocks,
  522. struct buffer_head *bh_result,
  523. int create)
  524. {
  525. int err = -EIO;
  526. int offsets[4];
  527. Indirect chain[4];
  528. Indirect *partial;
  529. ext2_fsblk_t goal;
  530. int indirect_blks;
  531. int blocks_to_boundary = 0;
  532. int depth;
  533. struct ext2_inode_info *ei = EXT2_I(inode);
  534. int count = 0;
  535. ext2_fsblk_t first_block = 0;
  536. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  537. if (depth == 0)
  538. return (err);
  539. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  540. /* Simplest case - block found, no allocation needed */
  541. if (!partial) {
  542. first_block = le32_to_cpu(chain[depth - 1].key);
  543. clear_buffer_new(bh_result); /* What's this do? */
  544. count++;
  545. /*map more blocks*/
  546. while (count < maxblocks && count <= blocks_to_boundary) {
  547. ext2_fsblk_t blk;
  548. if (!verify_chain(chain, chain + depth - 1)) {
  549. /*
  550. * Indirect block might be removed by
  551. * truncate while we were reading it.
  552. * Handling of that case: forget what we've
  553. * got now, go to reread.
  554. */
  555. err = -EAGAIN;
  556. count = 0;
  557. break;
  558. }
  559. blk = le32_to_cpu(*(chain[depth-1].p + count));
  560. if (blk == first_block + count)
  561. count++;
  562. else
  563. break;
  564. }
  565. if (err != -EAGAIN)
  566. goto got_it;
  567. }
  568. /* Next simple case - plain lookup or failed read of indirect block */
  569. if (!create || err == -EIO)
  570. goto cleanup;
  571. mutex_lock(&ei->truncate_mutex);
  572. /*
  573. * If the indirect block is missing while we are reading
  574. * the chain(ext3_get_branch() returns -EAGAIN err), or
  575. * if the chain has been changed after we grab the semaphore,
  576. * (either because another process truncated this branch, or
  577. * another get_block allocated this branch) re-grab the chain to see if
  578. * the request block has been allocated or not.
  579. *
  580. * Since we already block the truncate/other get_block
  581. * at this point, we will have the current copy of the chain when we
  582. * splice the branch into the tree.
  583. */
  584. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  585. while (partial > chain) {
  586. brelse(partial->bh);
  587. partial--;
  588. }
  589. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  590. if (!partial) {
  591. count++;
  592. mutex_unlock(&ei->truncate_mutex);
  593. if (err)
  594. goto cleanup;
  595. clear_buffer_new(bh_result);
  596. goto got_it;
  597. }
  598. }
  599. /*
  600. * Okay, we need to do block allocation. Lazily initialize the block
  601. * allocation info here if necessary
  602. */
  603. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  604. ext2_init_block_alloc_info(inode);
  605. goal = ext2_find_goal(inode, iblock, partial);
  606. /* the number of blocks need to allocate for [d,t]indirect blocks */
  607. indirect_blks = (chain + depth) - partial - 1;
  608. /*
  609. * Next look up the indirect map to count the totoal number of
  610. * direct blocks to allocate for this branch.
  611. */
  612. count = ext2_blks_to_allocate(partial, indirect_blks,
  613. maxblocks, blocks_to_boundary);
  614. /*
  615. * XXX ???? Block out ext2_truncate while we alter the tree
  616. */
  617. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  618. offsets + (partial - chain), partial);
  619. if (err) {
  620. mutex_unlock(&ei->truncate_mutex);
  621. goto cleanup;
  622. }
  623. if (ext2_use_xip(inode->i_sb)) {
  624. /*
  625. * we need to clear the block
  626. */
  627. err = ext2_clear_xip_target (inode,
  628. le32_to_cpu(chain[depth-1].key));
  629. if (err) {
  630. mutex_unlock(&ei->truncate_mutex);
  631. goto cleanup;
  632. }
  633. }
  634. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  635. mutex_unlock(&ei->truncate_mutex);
  636. set_buffer_new(bh_result);
  637. got_it:
  638. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  639. if (count > blocks_to_boundary)
  640. set_buffer_boundary(bh_result);
  641. err = count;
  642. /* Clean up and exit */
  643. partial = chain + depth - 1; /* the whole chain */
  644. cleanup:
  645. while (partial > chain) {
  646. brelse(partial->bh);
  647. partial--;
  648. }
  649. return err;
  650. }
  651. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  652. {
  653. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  654. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  655. bh_result, create);
  656. if (ret > 0) {
  657. bh_result->b_size = (ret << inode->i_blkbits);
  658. ret = 0;
  659. }
  660. return ret;
  661. }
  662. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  663. u64 start, u64 len)
  664. {
  665. return generic_block_fiemap(inode, fieinfo, start, len,
  666. ext2_get_block);
  667. }
  668. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  669. {
  670. return block_write_full_page(page, ext2_get_block, wbc);
  671. }
  672. static int ext2_readpage(struct file *file, struct page *page)
  673. {
  674. return mpage_readpage(page, ext2_get_block);
  675. }
  676. static int
  677. ext2_readpages(struct file *file, struct address_space *mapping,
  678. struct list_head *pages, unsigned nr_pages)
  679. {
  680. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  681. }
  682. int __ext2_write_begin(struct file *file, struct address_space *mapping,
  683. loff_t pos, unsigned len, unsigned flags,
  684. struct page **pagep, void **fsdata)
  685. {
  686. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  687. ext2_get_block);
  688. }
  689. static int
  690. ext2_write_begin(struct file *file, struct address_space *mapping,
  691. loff_t pos, unsigned len, unsigned flags,
  692. struct page **pagep, void **fsdata)
  693. {
  694. *pagep = NULL;
  695. return __ext2_write_begin(file, mapping, pos, len, flags, pagep,fsdata);
  696. }
  697. static int
  698. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  699. loff_t pos, unsigned len, unsigned flags,
  700. struct page **pagep, void **fsdata)
  701. {
  702. /*
  703. * Dir-in-pagecache still uses ext2_write_begin. Would have to rework
  704. * directory handling code to pass around offsets rather than struct
  705. * pages in order to make this work easily.
  706. */
  707. return nobh_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  708. ext2_get_block);
  709. }
  710. static int ext2_nobh_writepage(struct page *page,
  711. struct writeback_control *wbc)
  712. {
  713. return nobh_writepage(page, ext2_get_block, wbc);
  714. }
  715. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  716. {
  717. return generic_block_bmap(mapping,block,ext2_get_block);
  718. }
  719. static ssize_t
  720. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  721. loff_t offset, unsigned long nr_segs)
  722. {
  723. struct file *file = iocb->ki_filp;
  724. struct inode *inode = file->f_mapping->host;
  725. return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  726. offset, nr_segs, ext2_get_block, NULL);
  727. }
  728. static int
  729. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  730. {
  731. return mpage_writepages(mapping, wbc, ext2_get_block);
  732. }
  733. const struct address_space_operations ext2_aops = {
  734. .readpage = ext2_readpage,
  735. .readpages = ext2_readpages,
  736. .writepage = ext2_writepage,
  737. .sync_page = block_sync_page,
  738. .write_begin = ext2_write_begin,
  739. .write_end = generic_write_end,
  740. .bmap = ext2_bmap,
  741. .direct_IO = ext2_direct_IO,
  742. .writepages = ext2_writepages,
  743. .migratepage = buffer_migrate_page,
  744. .is_partially_uptodate = block_is_partially_uptodate,
  745. .error_remove_page = generic_error_remove_page,
  746. };
  747. const struct address_space_operations ext2_aops_xip = {
  748. .bmap = ext2_bmap,
  749. .get_xip_mem = ext2_get_xip_mem,
  750. };
  751. const struct address_space_operations ext2_nobh_aops = {
  752. .readpage = ext2_readpage,
  753. .readpages = ext2_readpages,
  754. .writepage = ext2_nobh_writepage,
  755. .sync_page = block_sync_page,
  756. .write_begin = ext2_nobh_write_begin,
  757. .write_end = nobh_write_end,
  758. .bmap = ext2_bmap,
  759. .direct_IO = ext2_direct_IO,
  760. .writepages = ext2_writepages,
  761. .migratepage = buffer_migrate_page,
  762. .error_remove_page = generic_error_remove_page,
  763. };
  764. /*
  765. * Probably it should be a library function... search for first non-zero word
  766. * or memcmp with zero_page, whatever is better for particular architecture.
  767. * Linus?
  768. */
  769. static inline int all_zeroes(__le32 *p, __le32 *q)
  770. {
  771. while (p < q)
  772. if (*p++)
  773. return 0;
  774. return 1;
  775. }
  776. /**
  777. * ext2_find_shared - find the indirect blocks for partial truncation.
  778. * @inode: inode in question
  779. * @depth: depth of the affected branch
  780. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  781. * @chain: place to store the pointers to partial indirect blocks
  782. * @top: place to the (detached) top of branch
  783. *
  784. * This is a helper function used by ext2_truncate().
  785. *
  786. * When we do truncate() we may have to clean the ends of several indirect
  787. * blocks but leave the blocks themselves alive. Block is partially
  788. * truncated if some data below the new i_size is refered from it (and
  789. * it is on the path to the first completely truncated data block, indeed).
  790. * We have to free the top of that path along with everything to the right
  791. * of the path. Since no allocation past the truncation point is possible
  792. * until ext2_truncate() finishes, we may safely do the latter, but top
  793. * of branch may require special attention - pageout below the truncation
  794. * point might try to populate it.
  795. *
  796. * We atomically detach the top of branch from the tree, store the block
  797. * number of its root in *@top, pointers to buffer_heads of partially
  798. * truncated blocks - in @chain[].bh and pointers to their last elements
  799. * that should not be removed - in @chain[].p. Return value is the pointer
  800. * to last filled element of @chain.
  801. *
  802. * The work left to caller to do the actual freeing of subtrees:
  803. * a) free the subtree starting from *@top
  804. * b) free the subtrees whose roots are stored in
  805. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  806. * c) free the subtrees growing from the inode past the @chain[0].p
  807. * (no partially truncated stuff there).
  808. */
  809. static Indirect *ext2_find_shared(struct inode *inode,
  810. int depth,
  811. int offsets[4],
  812. Indirect chain[4],
  813. __le32 *top)
  814. {
  815. Indirect *partial, *p;
  816. int k, err;
  817. *top = 0;
  818. for (k = depth; k > 1 && !offsets[k-1]; k--)
  819. ;
  820. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  821. if (!partial)
  822. partial = chain + k-1;
  823. /*
  824. * If the branch acquired continuation since we've looked at it -
  825. * fine, it should all survive and (new) top doesn't belong to us.
  826. */
  827. write_lock(&EXT2_I(inode)->i_meta_lock);
  828. if (!partial->key && *partial->p) {
  829. write_unlock(&EXT2_I(inode)->i_meta_lock);
  830. goto no_top;
  831. }
  832. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  833. ;
  834. /*
  835. * OK, we've found the last block that must survive. The rest of our
  836. * branch should be detached before unlocking. However, if that rest
  837. * of branch is all ours and does not grow immediately from the inode
  838. * it's easier to cheat and just decrement partial->p.
  839. */
  840. if (p == chain + k - 1 && p > chain) {
  841. p->p--;
  842. } else {
  843. *top = *p->p;
  844. *p->p = 0;
  845. }
  846. write_unlock(&EXT2_I(inode)->i_meta_lock);
  847. while(partial > p)
  848. {
  849. brelse(partial->bh);
  850. partial--;
  851. }
  852. no_top:
  853. return partial;
  854. }
  855. /**
  856. * ext2_free_data - free a list of data blocks
  857. * @inode: inode we are dealing with
  858. * @p: array of block numbers
  859. * @q: points immediately past the end of array
  860. *
  861. * We are freeing all blocks refered from that array (numbers are
  862. * stored as little-endian 32-bit) and updating @inode->i_blocks
  863. * appropriately.
  864. */
  865. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  866. {
  867. unsigned long block_to_free = 0, count = 0;
  868. unsigned long nr;
  869. for ( ; p < q ; p++) {
  870. nr = le32_to_cpu(*p);
  871. if (nr) {
  872. *p = 0;
  873. /* accumulate blocks to free if they're contiguous */
  874. if (count == 0)
  875. goto free_this;
  876. else if (block_to_free == nr - count)
  877. count++;
  878. else {
  879. mark_inode_dirty(inode);
  880. ext2_free_blocks (inode, block_to_free, count);
  881. free_this:
  882. block_to_free = nr;
  883. count = 1;
  884. }
  885. }
  886. }
  887. if (count > 0) {
  888. mark_inode_dirty(inode);
  889. ext2_free_blocks (inode, block_to_free, count);
  890. }
  891. }
  892. /**
  893. * ext2_free_branches - free an array of branches
  894. * @inode: inode we are dealing with
  895. * @p: array of block numbers
  896. * @q: pointer immediately past the end of array
  897. * @depth: depth of the branches to free
  898. *
  899. * We are freeing all blocks refered from these branches (numbers are
  900. * stored as little-endian 32-bit) and updating @inode->i_blocks
  901. * appropriately.
  902. */
  903. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  904. {
  905. struct buffer_head * bh;
  906. unsigned long nr;
  907. if (depth--) {
  908. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  909. for ( ; p < q ; p++) {
  910. nr = le32_to_cpu(*p);
  911. if (!nr)
  912. continue;
  913. *p = 0;
  914. bh = sb_bread(inode->i_sb, nr);
  915. /*
  916. * A read failure? Report error and clear slot
  917. * (should be rare).
  918. */
  919. if (!bh) {
  920. ext2_error(inode->i_sb, "ext2_free_branches",
  921. "Read failure, inode=%ld, block=%ld",
  922. inode->i_ino, nr);
  923. continue;
  924. }
  925. ext2_free_branches(inode,
  926. (__le32*)bh->b_data,
  927. (__le32*)bh->b_data + addr_per_block,
  928. depth);
  929. bforget(bh);
  930. ext2_free_blocks(inode, nr, 1);
  931. mark_inode_dirty(inode);
  932. }
  933. } else
  934. ext2_free_data(inode, p, q);
  935. }
  936. void ext2_truncate(struct inode *inode)
  937. {
  938. __le32 *i_data = EXT2_I(inode)->i_data;
  939. struct ext2_inode_info *ei = EXT2_I(inode);
  940. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  941. int offsets[4];
  942. Indirect chain[4];
  943. Indirect *partial;
  944. __le32 nr = 0;
  945. int n;
  946. long iblock;
  947. unsigned blocksize;
  948. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  949. S_ISLNK(inode->i_mode)))
  950. return;
  951. if (ext2_inode_is_fast_symlink(inode))
  952. return;
  953. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  954. return;
  955. blocksize = inode->i_sb->s_blocksize;
  956. iblock = (inode->i_size + blocksize-1)
  957. >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  958. if (mapping_is_xip(inode->i_mapping))
  959. xip_truncate_page(inode->i_mapping, inode->i_size);
  960. else if (test_opt(inode->i_sb, NOBH))
  961. nobh_truncate_page(inode->i_mapping,
  962. inode->i_size, ext2_get_block);
  963. else
  964. block_truncate_page(inode->i_mapping,
  965. inode->i_size, ext2_get_block);
  966. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  967. if (n == 0)
  968. return;
  969. /*
  970. * From here we block out all ext2_get_block() callers who want to
  971. * modify the block allocation tree.
  972. */
  973. mutex_lock(&ei->truncate_mutex);
  974. if (n == 1) {
  975. ext2_free_data(inode, i_data+offsets[0],
  976. i_data + EXT2_NDIR_BLOCKS);
  977. goto do_indirects;
  978. }
  979. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  980. /* Kill the top of shared branch (already detached) */
  981. if (nr) {
  982. if (partial == chain)
  983. mark_inode_dirty(inode);
  984. else
  985. mark_buffer_dirty_inode(partial->bh, inode);
  986. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  987. }
  988. /* Clear the ends of indirect blocks on the shared branch */
  989. while (partial > chain) {
  990. ext2_free_branches(inode,
  991. partial->p + 1,
  992. (__le32*)partial->bh->b_data+addr_per_block,
  993. (chain+n-1) - partial);
  994. mark_buffer_dirty_inode(partial->bh, inode);
  995. brelse (partial->bh);
  996. partial--;
  997. }
  998. do_indirects:
  999. /* Kill the remaining (whole) subtrees */
  1000. switch (offsets[0]) {
  1001. default:
  1002. nr = i_data[EXT2_IND_BLOCK];
  1003. if (nr) {
  1004. i_data[EXT2_IND_BLOCK] = 0;
  1005. mark_inode_dirty(inode);
  1006. ext2_free_branches(inode, &nr, &nr+1, 1);
  1007. }
  1008. case EXT2_IND_BLOCK:
  1009. nr = i_data[EXT2_DIND_BLOCK];
  1010. if (nr) {
  1011. i_data[EXT2_DIND_BLOCK] = 0;
  1012. mark_inode_dirty(inode);
  1013. ext2_free_branches(inode, &nr, &nr+1, 2);
  1014. }
  1015. case EXT2_DIND_BLOCK:
  1016. nr = i_data[EXT2_TIND_BLOCK];
  1017. if (nr) {
  1018. i_data[EXT2_TIND_BLOCK] = 0;
  1019. mark_inode_dirty(inode);
  1020. ext2_free_branches(inode, &nr, &nr+1, 3);
  1021. }
  1022. case EXT2_TIND_BLOCK:
  1023. ;
  1024. }
  1025. ext2_discard_reservation(inode);
  1026. mutex_unlock(&ei->truncate_mutex);
  1027. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1028. if (inode_needs_sync(inode)) {
  1029. sync_mapping_buffers(inode->i_mapping);
  1030. ext2_sync_inode (inode);
  1031. } else {
  1032. mark_inode_dirty(inode);
  1033. }
  1034. }
  1035. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1036. struct buffer_head **p)
  1037. {
  1038. struct buffer_head * bh;
  1039. unsigned long block_group;
  1040. unsigned long block;
  1041. unsigned long offset;
  1042. struct ext2_group_desc * gdp;
  1043. *p = NULL;
  1044. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1045. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1046. goto Einval;
  1047. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1048. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1049. if (!gdp)
  1050. goto Egdp;
  1051. /*
  1052. * Figure out the offset within the block group inode table
  1053. */
  1054. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1055. block = le32_to_cpu(gdp->bg_inode_table) +
  1056. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1057. if (!(bh = sb_bread(sb, block)))
  1058. goto Eio;
  1059. *p = bh;
  1060. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1061. return (struct ext2_inode *) (bh->b_data + offset);
  1062. Einval:
  1063. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1064. (unsigned long) ino);
  1065. return ERR_PTR(-EINVAL);
  1066. Eio:
  1067. ext2_error(sb, "ext2_get_inode",
  1068. "unable to read inode block - inode=%lu, block=%lu",
  1069. (unsigned long) ino, block);
  1070. Egdp:
  1071. return ERR_PTR(-EIO);
  1072. }
  1073. void ext2_set_inode_flags(struct inode *inode)
  1074. {
  1075. unsigned int flags = EXT2_I(inode)->i_flags;
  1076. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1077. if (flags & EXT2_SYNC_FL)
  1078. inode->i_flags |= S_SYNC;
  1079. if (flags & EXT2_APPEND_FL)
  1080. inode->i_flags |= S_APPEND;
  1081. if (flags & EXT2_IMMUTABLE_FL)
  1082. inode->i_flags |= S_IMMUTABLE;
  1083. if (flags & EXT2_NOATIME_FL)
  1084. inode->i_flags |= S_NOATIME;
  1085. if (flags & EXT2_DIRSYNC_FL)
  1086. inode->i_flags |= S_DIRSYNC;
  1087. }
  1088. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1089. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1090. {
  1091. unsigned int flags = ei->vfs_inode.i_flags;
  1092. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1093. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1094. if (flags & S_SYNC)
  1095. ei->i_flags |= EXT2_SYNC_FL;
  1096. if (flags & S_APPEND)
  1097. ei->i_flags |= EXT2_APPEND_FL;
  1098. if (flags & S_IMMUTABLE)
  1099. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1100. if (flags & S_NOATIME)
  1101. ei->i_flags |= EXT2_NOATIME_FL;
  1102. if (flags & S_DIRSYNC)
  1103. ei->i_flags |= EXT2_DIRSYNC_FL;
  1104. }
  1105. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1106. {
  1107. struct ext2_inode_info *ei;
  1108. struct buffer_head * bh;
  1109. struct ext2_inode *raw_inode;
  1110. struct inode *inode;
  1111. long ret = -EIO;
  1112. int n;
  1113. inode = iget_locked(sb, ino);
  1114. if (!inode)
  1115. return ERR_PTR(-ENOMEM);
  1116. if (!(inode->i_state & I_NEW))
  1117. return inode;
  1118. ei = EXT2_I(inode);
  1119. ei->i_block_alloc_info = NULL;
  1120. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1121. if (IS_ERR(raw_inode)) {
  1122. ret = PTR_ERR(raw_inode);
  1123. goto bad_inode;
  1124. }
  1125. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1126. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1127. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1128. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1129. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1130. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1131. }
  1132. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  1133. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1134. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1135. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1136. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1137. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1138. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1139. /* We now have enough fields to check if the inode was active or not.
  1140. * This is needed because nfsd might try to access dead inodes
  1141. * the test is that same one that e2fsck uses
  1142. * NeilBrown 1999oct15
  1143. */
  1144. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1145. /* this inode is deleted */
  1146. brelse (bh);
  1147. ret = -ESTALE;
  1148. goto bad_inode;
  1149. }
  1150. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1151. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1152. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1153. ei->i_frag_no = raw_inode->i_frag;
  1154. ei->i_frag_size = raw_inode->i_fsize;
  1155. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1156. ei->i_dir_acl = 0;
  1157. if (S_ISREG(inode->i_mode))
  1158. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1159. else
  1160. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1161. ei->i_dtime = 0;
  1162. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1163. ei->i_state = 0;
  1164. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1165. ei->i_dir_start_lookup = 0;
  1166. /*
  1167. * NOTE! The in-memory inode i_data array is in little-endian order
  1168. * even on big-endian machines: we do NOT byteswap the block numbers!
  1169. */
  1170. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1171. ei->i_data[n] = raw_inode->i_block[n];
  1172. if (S_ISREG(inode->i_mode)) {
  1173. inode->i_op = &ext2_file_inode_operations;
  1174. if (ext2_use_xip(inode->i_sb)) {
  1175. inode->i_mapping->a_ops = &ext2_aops_xip;
  1176. inode->i_fop = &ext2_xip_file_operations;
  1177. } else if (test_opt(inode->i_sb, NOBH)) {
  1178. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1179. inode->i_fop = &ext2_file_operations;
  1180. } else {
  1181. inode->i_mapping->a_ops = &ext2_aops;
  1182. inode->i_fop = &ext2_file_operations;
  1183. }
  1184. } else if (S_ISDIR(inode->i_mode)) {
  1185. inode->i_op = &ext2_dir_inode_operations;
  1186. inode->i_fop = &ext2_dir_operations;
  1187. if (test_opt(inode->i_sb, NOBH))
  1188. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1189. else
  1190. inode->i_mapping->a_ops = &ext2_aops;
  1191. } else if (S_ISLNK(inode->i_mode)) {
  1192. if (ext2_inode_is_fast_symlink(inode)) {
  1193. inode->i_op = &ext2_fast_symlink_inode_operations;
  1194. nd_terminate_link(ei->i_data, inode->i_size,
  1195. sizeof(ei->i_data) - 1);
  1196. } else {
  1197. inode->i_op = &ext2_symlink_inode_operations;
  1198. if (test_opt(inode->i_sb, NOBH))
  1199. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1200. else
  1201. inode->i_mapping->a_ops = &ext2_aops;
  1202. }
  1203. } else {
  1204. inode->i_op = &ext2_special_inode_operations;
  1205. if (raw_inode->i_block[0])
  1206. init_special_inode(inode, inode->i_mode,
  1207. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1208. else
  1209. init_special_inode(inode, inode->i_mode,
  1210. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1211. }
  1212. brelse (bh);
  1213. ext2_set_inode_flags(inode);
  1214. unlock_new_inode(inode);
  1215. return inode;
  1216. bad_inode:
  1217. iget_failed(inode);
  1218. return ERR_PTR(ret);
  1219. }
  1220. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1221. {
  1222. struct ext2_inode_info *ei = EXT2_I(inode);
  1223. struct super_block *sb = inode->i_sb;
  1224. ino_t ino = inode->i_ino;
  1225. uid_t uid = inode->i_uid;
  1226. gid_t gid = inode->i_gid;
  1227. struct buffer_head * bh;
  1228. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1229. int n;
  1230. int err = 0;
  1231. if (IS_ERR(raw_inode))
  1232. return -EIO;
  1233. /* For fields not not tracking in the in-memory inode,
  1234. * initialise them to zero for new inodes. */
  1235. if (ei->i_state & EXT2_STATE_NEW)
  1236. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1237. ext2_get_inode_flags(ei);
  1238. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1239. if (!(test_opt(sb, NO_UID32))) {
  1240. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1241. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1242. /*
  1243. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1244. * re-used with the upper 16 bits of the uid/gid intact
  1245. */
  1246. if (!ei->i_dtime) {
  1247. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1248. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1249. } else {
  1250. raw_inode->i_uid_high = 0;
  1251. raw_inode->i_gid_high = 0;
  1252. }
  1253. } else {
  1254. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1255. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1256. raw_inode->i_uid_high = 0;
  1257. raw_inode->i_gid_high = 0;
  1258. }
  1259. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1260. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1261. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1262. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1263. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1264. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1265. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1266. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1267. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1268. raw_inode->i_frag = ei->i_frag_no;
  1269. raw_inode->i_fsize = ei->i_frag_size;
  1270. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1271. if (!S_ISREG(inode->i_mode))
  1272. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1273. else {
  1274. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1275. if (inode->i_size > 0x7fffffffULL) {
  1276. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1277. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1278. EXT2_SB(sb)->s_es->s_rev_level ==
  1279. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1280. /* If this is the first large file
  1281. * created, add a flag to the superblock.
  1282. */
  1283. lock_kernel();
  1284. ext2_update_dynamic_rev(sb);
  1285. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1286. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1287. unlock_kernel();
  1288. ext2_write_super(sb);
  1289. }
  1290. }
  1291. }
  1292. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1293. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1294. if (old_valid_dev(inode->i_rdev)) {
  1295. raw_inode->i_block[0] =
  1296. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1297. raw_inode->i_block[1] = 0;
  1298. } else {
  1299. raw_inode->i_block[0] = 0;
  1300. raw_inode->i_block[1] =
  1301. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1302. raw_inode->i_block[2] = 0;
  1303. }
  1304. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1305. raw_inode->i_block[n] = ei->i_data[n];
  1306. mark_buffer_dirty(bh);
  1307. if (do_sync) {
  1308. sync_dirty_buffer(bh);
  1309. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1310. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1311. sb->s_id, (unsigned long) ino);
  1312. err = -EIO;
  1313. }
  1314. }
  1315. ei->i_state &= ~EXT2_STATE_NEW;
  1316. brelse (bh);
  1317. return err;
  1318. }
  1319. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1320. {
  1321. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1322. }
  1323. int ext2_sync_inode(struct inode *inode)
  1324. {
  1325. struct writeback_control wbc = {
  1326. .sync_mode = WB_SYNC_ALL,
  1327. .nr_to_write = 0, /* sys_fsync did this */
  1328. };
  1329. return sync_inode(inode, &wbc);
  1330. }
  1331. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1332. {
  1333. struct inode *inode = dentry->d_inode;
  1334. int error;
  1335. error = inode_change_ok(inode, iattr);
  1336. if (error)
  1337. return error;
  1338. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  1339. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  1340. error = vfs_dq_transfer(inode, iattr) ? -EDQUOT : 0;
  1341. if (error)
  1342. return error;
  1343. }
  1344. error = inode_setattr(inode, iattr);
  1345. if (!error && (iattr->ia_valid & ATTR_MODE))
  1346. error = ext2_acl_chmod(inode);
  1347. return error;
  1348. }