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_update_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_update_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_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
  127. } else if (i_block < direct_blocks) {
  128. offsets[n++] = i_block;
  129. final = direct_blocks;
  130. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  131. offsets[n++] = EXT2_IND_BLOCK;
  132. offsets[n++] = i_block;
  133. final = ptrs;
  134. } else if ((i_block -= indirect_blocks) < double_blocks) {
  135. offsets[n++] = EXT2_DIND_BLOCK;
  136. offsets[n++] = i_block >> ptrs_bits;
  137. offsets[n++] = i_block & (ptrs - 1);
  138. final = ptrs;
  139. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  140. offsets[n++] = EXT2_TIND_BLOCK;
  141. offsets[n++] = i_block >> (ptrs_bits * 2);
  142. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  143. offsets[n++] = i_block & (ptrs - 1);
  144. final = ptrs;
  145. } else {
  146. ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
  147. }
  148. if (boundary)
  149. *boundary = final - 1 - (i_block & (ptrs - 1));
  150. return n;
  151. }
  152. /**
  153. * ext2_get_branch - read the chain of indirect blocks leading to data
  154. * @inode: inode in question
  155. * @depth: depth of the chain (1 - direct pointer, etc.)
  156. * @offsets: offsets of pointers in inode/indirect blocks
  157. * @chain: place to store the result
  158. * @err: here we store the error value
  159. *
  160. * Function fills the array of triples <key, p, bh> and returns %NULL
  161. * if everything went OK or the pointer to the last filled triple
  162. * (incomplete one) otherwise. Upon the return chain[i].key contains
  163. * the number of (i+1)-th block in the chain (as it is stored in memory,
  164. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  165. * number (it points into struct inode for i==0 and into the bh->b_data
  166. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  167. * block for i>0 and NULL for i==0. In other words, it holds the block
  168. * numbers of the chain, addresses they were taken from (and where we can
  169. * verify that chain did not change) and buffer_heads hosting these
  170. * numbers.
  171. *
  172. * Function stops when it stumbles upon zero pointer (absent block)
  173. * (pointer to last triple returned, *@err == 0)
  174. * or when it gets an IO error reading an indirect block
  175. * (ditto, *@err == -EIO)
  176. * or when it notices that chain had been changed while it was reading
  177. * (ditto, *@err == -EAGAIN)
  178. * or when it reads all @depth-1 indirect blocks successfully and finds
  179. * the whole chain, all way to the data (returns %NULL, *err == 0).
  180. */
  181. static Indirect *ext2_get_branch(struct inode *inode,
  182. int depth,
  183. int *offsets,
  184. Indirect chain[4],
  185. int *err)
  186. {
  187. struct super_block *sb = inode->i_sb;
  188. Indirect *p = chain;
  189. struct buffer_head *bh;
  190. *err = 0;
  191. /* i_data is not going away, no lock needed */
  192. add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
  193. if (!p->key)
  194. goto no_block;
  195. while (--depth) {
  196. bh = sb_bread(sb, le32_to_cpu(p->key));
  197. if (!bh)
  198. goto failure;
  199. read_lock(&EXT2_I(inode)->i_meta_lock);
  200. if (!verify_chain(chain, p))
  201. goto changed;
  202. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  203. read_unlock(&EXT2_I(inode)->i_meta_lock);
  204. if (!p->key)
  205. goto no_block;
  206. }
  207. return NULL;
  208. changed:
  209. read_unlock(&EXT2_I(inode)->i_meta_lock);
  210. brelse(bh);
  211. *err = -EAGAIN;
  212. goto no_block;
  213. failure:
  214. *err = -EIO;
  215. no_block:
  216. return p;
  217. }
  218. /**
  219. * ext2_find_near - find a place for allocation with sufficient locality
  220. * @inode: owner
  221. * @ind: descriptor of indirect block.
  222. *
  223. * This function returns the preferred place for block allocation.
  224. * It is used when heuristic for sequential allocation fails.
  225. * Rules are:
  226. * + if there is a block to the left of our position - allocate near it.
  227. * + if pointer will live in indirect block - allocate near that block.
  228. * + if pointer will live in inode - allocate in the same cylinder group.
  229. *
  230. * In the latter case we colour the starting block by the callers PID to
  231. * prevent it from clashing with concurrent allocations for a different inode
  232. * in the same block group. The PID is used here so that functionally related
  233. * files will be close-by on-disk.
  234. *
  235. * Caller must make sure that @ind is valid and will stay that way.
  236. */
  237. static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
  238. {
  239. struct ext2_inode_info *ei = EXT2_I(inode);
  240. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  241. __le32 *p;
  242. ext2_fsblk_t bg_start;
  243. ext2_fsblk_t colour;
  244. /* Try to find previous block */
  245. for (p = ind->p - 1; p >= start; p--)
  246. if (*p)
  247. return le32_to_cpu(*p);
  248. /* No such thing, so let's try location of indirect block */
  249. if (ind->bh)
  250. return ind->bh->b_blocknr;
  251. /*
  252. * It is going to be refered from inode itself? OK, just put it into
  253. * the same cylinder group then.
  254. */
  255. bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
  256. colour = (current->pid % 16) *
  257. (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  258. return bg_start + colour;
  259. }
  260. /**
  261. * ext2_find_goal - find a preferred place for allocation.
  262. * @inode: owner
  263. * @block: block we want
  264. * @partial: pointer to the last triple within a chain
  265. *
  266. * Returns preferred place for a block (the goal).
  267. */
  268. static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
  269. Indirect *partial)
  270. {
  271. struct ext2_block_alloc_info *block_i;
  272. block_i = EXT2_I(inode)->i_block_alloc_info;
  273. /*
  274. * try the heuristic for sequential allocation,
  275. * failing that at least try to get decent locality.
  276. */
  277. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  278. && (block_i->last_alloc_physical_block != 0)) {
  279. return block_i->last_alloc_physical_block + 1;
  280. }
  281. return ext2_find_near(inode, partial);
  282. }
  283. /**
  284. * ext2_blks_to_allocate: Look up the block map and count the number
  285. * of direct blocks need to be allocated for the given branch.
  286. *
  287. * @branch: chain of indirect blocks
  288. * @k: number of blocks need for indirect blocks
  289. * @blks: number of data blocks to be mapped.
  290. * @blocks_to_boundary: the offset in the indirect block
  291. *
  292. * return the total number of blocks to be allocate, including the
  293. * direct and indirect blocks.
  294. */
  295. static int
  296. ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
  297. int blocks_to_boundary)
  298. {
  299. unsigned long count = 0;
  300. /*
  301. * Simple case, [t,d]Indirect block(s) has not allocated yet
  302. * then it's clear blocks on that path have not allocated
  303. */
  304. if (k > 0) {
  305. /* right now don't hanel cross boundary allocation */
  306. if (blks < blocks_to_boundary + 1)
  307. count += blks;
  308. else
  309. count += blocks_to_boundary + 1;
  310. return count;
  311. }
  312. count++;
  313. while (count < blks && count <= blocks_to_boundary
  314. && le32_to_cpu(*(branch[0].p + count)) == 0) {
  315. count++;
  316. }
  317. return count;
  318. }
  319. /**
  320. * ext2_alloc_blocks: multiple allocate blocks needed for a branch
  321. * @indirect_blks: the number of blocks need to allocate for indirect
  322. * blocks
  323. *
  324. * @new_blocks: on return it will store the new block numbers for
  325. * the indirect blocks(if needed) and the first direct block,
  326. * @blks: on return it will store the total number of allocated
  327. * direct blocks
  328. */
  329. static int ext2_alloc_blocks(struct inode *inode,
  330. ext2_fsblk_t goal, int indirect_blks, int blks,
  331. ext2_fsblk_t new_blocks[4], int *err)
  332. {
  333. int target, i;
  334. unsigned long count = 0;
  335. int index = 0;
  336. ext2_fsblk_t current_block = 0;
  337. int ret = 0;
  338. /*
  339. * Here we try to allocate the requested multiple blocks at once,
  340. * on a best-effort basis.
  341. * To build a branch, we should allocate blocks for
  342. * the indirect blocks(if not allocated yet), and at least
  343. * the first direct block of this branch. That's the
  344. * minimum number of blocks need to allocate(required)
  345. */
  346. target = blks + indirect_blks;
  347. while (1) {
  348. count = target;
  349. /* allocating blocks for indirect blocks and direct blocks */
  350. current_block = ext2_new_blocks(inode,goal,&count,err);
  351. if (*err)
  352. goto failed_out;
  353. target -= count;
  354. /* allocate blocks for indirect blocks */
  355. while (index < indirect_blks && count) {
  356. new_blocks[index++] = current_block++;
  357. count--;
  358. }
  359. if (count > 0)
  360. break;
  361. }
  362. /* save the new block number for the first direct block */
  363. new_blocks[index] = current_block;
  364. /* total number of blocks allocated for direct blocks */
  365. ret = count;
  366. *err = 0;
  367. return ret;
  368. failed_out:
  369. for (i = 0; i <index; i++)
  370. ext2_free_blocks(inode, new_blocks[i], 1);
  371. return ret;
  372. }
  373. /**
  374. * ext2_alloc_branch - allocate and set up a chain of blocks.
  375. * @inode: owner
  376. * @num: depth of the chain (number of blocks to allocate)
  377. * @offsets: offsets (in the blocks) to store the pointers to next.
  378. * @branch: place to store the chain in.
  379. *
  380. * This function allocates @num blocks, zeroes out all but the last one,
  381. * links them into chain and (if we are synchronous) writes them to disk.
  382. * In other words, it prepares a branch that can be spliced onto the
  383. * inode. It stores the information about that chain in the branch[], in
  384. * the same format as ext2_get_branch() would do. We are calling it after
  385. * we had read the existing part of chain and partial points to the last
  386. * triple of that (one with zero ->key). Upon the exit we have the same
  387. * picture as after the successful ext2_get_block(), excpet that in one
  388. * place chain is disconnected - *branch->p is still zero (we did not
  389. * set the last link), but branch->key contains the number that should
  390. * be placed into *branch->p to fill that gap.
  391. *
  392. * If allocation fails we free all blocks we've allocated (and forget
  393. * their buffer_heads) and return the error value the from failed
  394. * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  395. * as described above and return 0.
  396. */
  397. static int ext2_alloc_branch(struct inode *inode,
  398. int indirect_blks, int *blks, ext2_fsblk_t goal,
  399. int *offsets, Indirect *branch)
  400. {
  401. int blocksize = inode->i_sb->s_blocksize;
  402. int i, n = 0;
  403. int err = 0;
  404. struct buffer_head *bh;
  405. int num;
  406. ext2_fsblk_t new_blocks[4];
  407. ext2_fsblk_t current_block;
  408. num = ext2_alloc_blocks(inode, goal, indirect_blks,
  409. *blks, new_blocks, &err);
  410. if (err)
  411. return err;
  412. branch[0].key = cpu_to_le32(new_blocks[0]);
  413. /*
  414. * metadata blocks and data blocks are allocated.
  415. */
  416. for (n = 1; n <= indirect_blks; n++) {
  417. /*
  418. * Get buffer_head for parent block, zero it out
  419. * and set the pointer to new one, then send
  420. * parent to disk.
  421. */
  422. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  423. branch[n].bh = bh;
  424. lock_buffer(bh);
  425. memset(bh->b_data, 0, blocksize);
  426. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  427. branch[n].key = cpu_to_le32(new_blocks[n]);
  428. *branch[n].p = branch[n].key;
  429. if ( n == indirect_blks) {
  430. current_block = new_blocks[n];
  431. /*
  432. * End of chain, update the last new metablock of
  433. * the chain to point to the new allocated
  434. * data blocks numbers
  435. */
  436. for (i=1; i < num; i++)
  437. *(branch[n].p + i) = cpu_to_le32(++current_block);
  438. }
  439. set_buffer_uptodate(bh);
  440. unlock_buffer(bh);
  441. mark_buffer_dirty_inode(bh, inode);
  442. /* We used to sync bh here if IS_SYNC(inode).
  443. * But we now rely upon generic_osync_inode()
  444. * and b_inode_buffers. But not for directories.
  445. */
  446. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  447. sync_dirty_buffer(bh);
  448. }
  449. *blks = num;
  450. return err;
  451. }
  452. /**
  453. * ext2_splice_branch - splice the allocated branch onto inode.
  454. * @inode: owner
  455. * @block: (logical) number of block we are adding
  456. * @where: location of missing link
  457. * @num: number of indirect blocks we are adding
  458. * @blks: number of direct blocks we are adding
  459. *
  460. * This function fills the missing link and does all housekeeping needed in
  461. * inode (->i_blocks, etc.). In case of success we end up with the full
  462. * chain to new block and return 0.
  463. */
  464. static void ext2_splice_branch(struct inode *inode,
  465. long block, Indirect *where, int num, int blks)
  466. {
  467. int i;
  468. struct ext2_block_alloc_info *block_i;
  469. ext2_fsblk_t current_block;
  470. block_i = EXT2_I(inode)->i_block_alloc_info;
  471. /* XXX LOCKING probably should have i_meta_lock ?*/
  472. /* That's it */
  473. *where->p = where->key;
  474. /*
  475. * Update the host buffer_head or inode to point to more just allocated
  476. * direct blocks blocks
  477. */
  478. if (num == 0 && blks > 1) {
  479. current_block = le32_to_cpu(where->key) + 1;
  480. for (i = 1; i < blks; i++)
  481. *(where->p + i ) = cpu_to_le32(current_block++);
  482. }
  483. /*
  484. * update the most recently allocated logical & physical block
  485. * in i_block_alloc_info, to assist find the proper goal block for next
  486. * allocation
  487. */
  488. if (block_i) {
  489. block_i->last_alloc_logical_block = block + blks - 1;
  490. block_i->last_alloc_physical_block =
  491. le32_to_cpu(where[num].key) + blks - 1;
  492. }
  493. /* We are done with atomic stuff, now do the rest of housekeeping */
  494. /* had we spliced it onto indirect block? */
  495. if (where->bh)
  496. mark_buffer_dirty_inode(where->bh, inode);
  497. inode->i_ctime = CURRENT_TIME_SEC;
  498. mark_inode_dirty(inode);
  499. }
  500. /*
  501. * Allocation strategy is simple: if we have to allocate something, we will
  502. * have to go the whole way to leaf. So let's do it before attaching anything
  503. * to tree, set linkage between the newborn blocks, write them if sync is
  504. * required, recheck the path, free and repeat if check fails, otherwise
  505. * set the last missing link (that will protect us from any truncate-generated
  506. * removals - all blocks on the path are immune now) and possibly force the
  507. * write on the parent block.
  508. * That has a nice additional property: no special recovery from the failed
  509. * allocations is needed - we simply release blocks and do not touch anything
  510. * reachable from inode.
  511. *
  512. * `handle' can be NULL if create == 0.
  513. *
  514. * return > 0, # of blocks mapped or allocated.
  515. * return = 0, if plain lookup failed.
  516. * return < 0, error case.
  517. */
  518. static int ext2_get_blocks(struct inode *inode,
  519. sector_t iblock, unsigned long maxblocks,
  520. struct buffer_head *bh_result,
  521. int create)
  522. {
  523. int err = -EIO;
  524. int offsets[4];
  525. Indirect chain[4];
  526. Indirect *partial;
  527. ext2_fsblk_t goal;
  528. int indirect_blks;
  529. int blocks_to_boundary = 0;
  530. int depth;
  531. struct ext2_inode_info *ei = EXT2_I(inode);
  532. int count = 0;
  533. ext2_fsblk_t first_block = 0;
  534. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  535. if (depth == 0)
  536. return (err);
  537. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  538. /* Simplest case - block found, no allocation needed */
  539. if (!partial) {
  540. first_block = le32_to_cpu(chain[depth - 1].key);
  541. clear_buffer_new(bh_result); /* What's this do? */
  542. count++;
  543. /*map more blocks*/
  544. while (count < maxblocks && count <= blocks_to_boundary) {
  545. ext2_fsblk_t blk;
  546. if (!verify_chain(chain, chain + depth - 1)) {
  547. /*
  548. * Indirect block might be removed by
  549. * truncate while we were reading it.
  550. * Handling of that case: forget what we've
  551. * got now, go to reread.
  552. */
  553. err = -EAGAIN;
  554. count = 0;
  555. break;
  556. }
  557. blk = le32_to_cpu(*(chain[depth-1].p + count));
  558. if (blk == first_block + count)
  559. count++;
  560. else
  561. break;
  562. }
  563. if (err != -EAGAIN)
  564. goto got_it;
  565. }
  566. /* Next simple case - plain lookup or failed read of indirect block */
  567. if (!create || err == -EIO)
  568. goto cleanup;
  569. mutex_lock(&ei->truncate_mutex);
  570. /*
  571. * If the indirect block is missing while we are reading
  572. * the chain(ext3_get_branch() returns -EAGAIN err), or
  573. * if the chain has been changed after we grab the semaphore,
  574. * (either because another process truncated this branch, or
  575. * another get_block allocated this branch) re-grab the chain to see if
  576. * the request block has been allocated or not.
  577. *
  578. * Since we already block the truncate/other get_block
  579. * at this point, we will have the current copy of the chain when we
  580. * splice the branch into the tree.
  581. */
  582. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  583. while (partial > chain) {
  584. brelse(partial->bh);
  585. partial--;
  586. }
  587. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  588. if (!partial) {
  589. count++;
  590. mutex_unlock(&ei->truncate_mutex);
  591. if (err)
  592. goto cleanup;
  593. clear_buffer_new(bh_result);
  594. goto got_it;
  595. }
  596. }
  597. /*
  598. * Okay, we need to do block allocation. Lazily initialize the block
  599. * allocation info here if necessary
  600. */
  601. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  602. ext2_init_block_alloc_info(inode);
  603. goal = ext2_find_goal(inode, iblock, partial);
  604. /* the number of blocks need to allocate for [d,t]indirect blocks */
  605. indirect_blks = (chain + depth) - partial - 1;
  606. /*
  607. * Next look up the indirect map to count the totoal number of
  608. * direct blocks to allocate for this branch.
  609. */
  610. count = ext2_blks_to_allocate(partial, indirect_blks,
  611. maxblocks, blocks_to_boundary);
  612. /*
  613. * XXX ???? Block out ext2_truncate while we alter the tree
  614. */
  615. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  616. offsets + (partial - chain), partial);
  617. if (err) {
  618. mutex_unlock(&ei->truncate_mutex);
  619. goto cleanup;
  620. }
  621. if (ext2_use_xip(inode->i_sb)) {
  622. /*
  623. * we need to clear the block
  624. */
  625. err = ext2_clear_xip_target (inode,
  626. le32_to_cpu(chain[depth-1].key));
  627. if (err) {
  628. mutex_unlock(&ei->truncate_mutex);
  629. goto cleanup;
  630. }
  631. }
  632. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  633. mutex_unlock(&ei->truncate_mutex);
  634. set_buffer_new(bh_result);
  635. got_it:
  636. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  637. if (count > blocks_to_boundary)
  638. set_buffer_boundary(bh_result);
  639. err = count;
  640. /* Clean up and exit */
  641. partial = chain + depth - 1; /* the whole chain */
  642. cleanup:
  643. while (partial > chain) {
  644. brelse(partial->bh);
  645. partial--;
  646. }
  647. return err;
  648. }
  649. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  650. {
  651. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  652. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  653. bh_result, create);
  654. if (ret > 0) {
  655. bh_result->b_size = (ret << inode->i_blkbits);
  656. ret = 0;
  657. }
  658. return ret;
  659. }
  660. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  661. u64 start, u64 len)
  662. {
  663. return generic_block_fiemap(inode, fieinfo, start, len,
  664. ext2_get_block);
  665. }
  666. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  667. {
  668. return block_write_full_page(page, ext2_get_block, wbc);
  669. }
  670. static int ext2_readpage(struct file *file, struct page *page)
  671. {
  672. return mpage_readpage(page, ext2_get_block);
  673. }
  674. static int
  675. ext2_readpages(struct file *file, struct address_space *mapping,
  676. struct list_head *pages, unsigned nr_pages)
  677. {
  678. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  679. }
  680. int __ext2_write_begin(struct file *file, struct address_space *mapping,
  681. loff_t pos, unsigned len, unsigned flags,
  682. struct page **pagep, void **fsdata)
  683. {
  684. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  685. ext2_get_block);
  686. }
  687. static int
  688. ext2_write_begin(struct file *file, struct address_space *mapping,
  689. loff_t pos, unsigned len, unsigned flags,
  690. struct page **pagep, void **fsdata)
  691. {
  692. *pagep = NULL;
  693. return __ext2_write_begin(file, mapping, pos, len, flags, pagep,fsdata);
  694. }
  695. static int
  696. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  697. loff_t pos, unsigned len, unsigned flags,
  698. struct page **pagep, void **fsdata)
  699. {
  700. /*
  701. * Dir-in-pagecache still uses ext2_write_begin. Would have to rework
  702. * directory handling code to pass around offsets rather than struct
  703. * pages in order to make this work easily.
  704. */
  705. return nobh_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  706. ext2_get_block);
  707. }
  708. static int ext2_nobh_writepage(struct page *page,
  709. struct writeback_control *wbc)
  710. {
  711. return nobh_writepage(page, ext2_get_block, wbc);
  712. }
  713. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  714. {
  715. return generic_block_bmap(mapping,block,ext2_get_block);
  716. }
  717. static ssize_t
  718. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  719. loff_t offset, unsigned long nr_segs)
  720. {
  721. struct file *file = iocb->ki_filp;
  722. struct inode *inode = file->f_mapping->host;
  723. return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  724. offset, nr_segs, ext2_get_block, NULL);
  725. }
  726. static int
  727. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  728. {
  729. return mpage_writepages(mapping, wbc, ext2_get_block);
  730. }
  731. const struct address_space_operations ext2_aops = {
  732. .readpage = ext2_readpage,
  733. .readpages = ext2_readpages,
  734. .writepage = ext2_writepage,
  735. .sync_page = block_sync_page,
  736. .write_begin = ext2_write_begin,
  737. .write_end = generic_write_end,
  738. .bmap = ext2_bmap,
  739. .direct_IO = ext2_direct_IO,
  740. .writepages = ext2_writepages,
  741. .migratepage = buffer_migrate_page,
  742. .is_partially_uptodate = block_is_partially_uptodate,
  743. };
  744. const struct address_space_operations ext2_aops_xip = {
  745. .bmap = ext2_bmap,
  746. .get_xip_mem = ext2_get_xip_mem,
  747. };
  748. const struct address_space_operations ext2_nobh_aops = {
  749. .readpage = ext2_readpage,
  750. .readpages = ext2_readpages,
  751. .writepage = ext2_nobh_writepage,
  752. .sync_page = block_sync_page,
  753. .write_begin = ext2_nobh_write_begin,
  754. .write_end = nobh_write_end,
  755. .bmap = ext2_bmap,
  756. .direct_IO = ext2_direct_IO,
  757. .writepages = ext2_writepages,
  758. .migratepage = buffer_migrate_page,
  759. };
  760. /*
  761. * Probably it should be a library function... search for first non-zero word
  762. * or memcmp with zero_page, whatever is better for particular architecture.
  763. * Linus?
  764. */
  765. static inline int all_zeroes(__le32 *p, __le32 *q)
  766. {
  767. while (p < q)
  768. if (*p++)
  769. return 0;
  770. return 1;
  771. }
  772. /**
  773. * ext2_find_shared - find the indirect blocks for partial truncation.
  774. * @inode: inode in question
  775. * @depth: depth of the affected branch
  776. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  777. * @chain: place to store the pointers to partial indirect blocks
  778. * @top: place to the (detached) top of branch
  779. *
  780. * This is a helper function used by ext2_truncate().
  781. *
  782. * When we do truncate() we may have to clean the ends of several indirect
  783. * blocks but leave the blocks themselves alive. Block is partially
  784. * truncated if some data below the new i_size is refered from it (and
  785. * it is on the path to the first completely truncated data block, indeed).
  786. * We have to free the top of that path along with everything to the right
  787. * of the path. Since no allocation past the truncation point is possible
  788. * until ext2_truncate() finishes, we may safely do the latter, but top
  789. * of branch may require special attention - pageout below the truncation
  790. * point might try to populate it.
  791. *
  792. * We atomically detach the top of branch from the tree, store the block
  793. * number of its root in *@top, pointers to buffer_heads of partially
  794. * truncated blocks - in @chain[].bh and pointers to their last elements
  795. * that should not be removed - in @chain[].p. Return value is the pointer
  796. * to last filled element of @chain.
  797. *
  798. * The work left to caller to do the actual freeing of subtrees:
  799. * a) free the subtree starting from *@top
  800. * b) free the subtrees whose roots are stored in
  801. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  802. * c) free the subtrees growing from the inode past the @chain[0].p
  803. * (no partially truncated stuff there).
  804. */
  805. static Indirect *ext2_find_shared(struct inode *inode,
  806. int depth,
  807. int offsets[4],
  808. Indirect chain[4],
  809. __le32 *top)
  810. {
  811. Indirect *partial, *p;
  812. int k, err;
  813. *top = 0;
  814. for (k = depth; k > 1 && !offsets[k-1]; k--)
  815. ;
  816. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  817. if (!partial)
  818. partial = chain + k-1;
  819. /*
  820. * If the branch acquired continuation since we've looked at it -
  821. * fine, it should all survive and (new) top doesn't belong to us.
  822. */
  823. write_lock(&EXT2_I(inode)->i_meta_lock);
  824. if (!partial->key && *partial->p) {
  825. write_unlock(&EXT2_I(inode)->i_meta_lock);
  826. goto no_top;
  827. }
  828. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  829. ;
  830. /*
  831. * OK, we've found the last block that must survive. The rest of our
  832. * branch should be detached before unlocking. However, if that rest
  833. * of branch is all ours and does not grow immediately from the inode
  834. * it's easier to cheat and just decrement partial->p.
  835. */
  836. if (p == chain + k - 1 && p > chain) {
  837. p->p--;
  838. } else {
  839. *top = *p->p;
  840. *p->p = 0;
  841. }
  842. write_unlock(&EXT2_I(inode)->i_meta_lock);
  843. while(partial > p)
  844. {
  845. brelse(partial->bh);
  846. partial--;
  847. }
  848. no_top:
  849. return partial;
  850. }
  851. /**
  852. * ext2_free_data - free a list of data blocks
  853. * @inode: inode we are dealing with
  854. * @p: array of block numbers
  855. * @q: points immediately past the end of array
  856. *
  857. * We are freeing all blocks refered from that array (numbers are
  858. * stored as little-endian 32-bit) and updating @inode->i_blocks
  859. * appropriately.
  860. */
  861. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  862. {
  863. unsigned long block_to_free = 0, count = 0;
  864. unsigned long nr;
  865. for ( ; p < q ; p++) {
  866. nr = le32_to_cpu(*p);
  867. if (nr) {
  868. *p = 0;
  869. /* accumulate blocks to free if they're contiguous */
  870. if (count == 0)
  871. goto free_this;
  872. else if (block_to_free == nr - count)
  873. count++;
  874. else {
  875. mark_inode_dirty(inode);
  876. ext2_free_blocks (inode, block_to_free, count);
  877. free_this:
  878. block_to_free = nr;
  879. count = 1;
  880. }
  881. }
  882. }
  883. if (count > 0) {
  884. mark_inode_dirty(inode);
  885. ext2_free_blocks (inode, block_to_free, count);
  886. }
  887. }
  888. /**
  889. * ext2_free_branches - free an array of branches
  890. * @inode: inode we are dealing with
  891. * @p: array of block numbers
  892. * @q: pointer immediately past the end of array
  893. * @depth: depth of the branches to free
  894. *
  895. * We are freeing all blocks refered from these branches (numbers are
  896. * stored as little-endian 32-bit) and updating @inode->i_blocks
  897. * appropriately.
  898. */
  899. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  900. {
  901. struct buffer_head * bh;
  902. unsigned long nr;
  903. if (depth--) {
  904. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  905. for ( ; p < q ; p++) {
  906. nr = le32_to_cpu(*p);
  907. if (!nr)
  908. continue;
  909. *p = 0;
  910. bh = sb_bread(inode->i_sb, nr);
  911. /*
  912. * A read failure? Report error and clear slot
  913. * (should be rare).
  914. */
  915. if (!bh) {
  916. ext2_error(inode->i_sb, "ext2_free_branches",
  917. "Read failure, inode=%ld, block=%ld",
  918. inode->i_ino, nr);
  919. continue;
  920. }
  921. ext2_free_branches(inode,
  922. (__le32*)bh->b_data,
  923. (__le32*)bh->b_data + addr_per_block,
  924. depth);
  925. bforget(bh);
  926. ext2_free_blocks(inode, nr, 1);
  927. mark_inode_dirty(inode);
  928. }
  929. } else
  930. ext2_free_data(inode, p, q);
  931. }
  932. void ext2_truncate(struct inode *inode)
  933. {
  934. __le32 *i_data = EXT2_I(inode)->i_data;
  935. struct ext2_inode_info *ei = EXT2_I(inode);
  936. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  937. int offsets[4];
  938. Indirect chain[4];
  939. Indirect *partial;
  940. __le32 nr = 0;
  941. int n;
  942. long iblock;
  943. unsigned blocksize;
  944. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  945. S_ISLNK(inode->i_mode)))
  946. return;
  947. if (ext2_inode_is_fast_symlink(inode))
  948. return;
  949. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  950. return;
  951. blocksize = inode->i_sb->s_blocksize;
  952. iblock = (inode->i_size + blocksize-1)
  953. >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  954. if (mapping_is_xip(inode->i_mapping))
  955. xip_truncate_page(inode->i_mapping, inode->i_size);
  956. else if (test_opt(inode->i_sb, NOBH))
  957. nobh_truncate_page(inode->i_mapping,
  958. inode->i_size, ext2_get_block);
  959. else
  960. block_truncate_page(inode->i_mapping,
  961. inode->i_size, ext2_get_block);
  962. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  963. if (n == 0)
  964. return;
  965. /*
  966. * From here we block out all ext2_get_block() callers who want to
  967. * modify the block allocation tree.
  968. */
  969. mutex_lock(&ei->truncate_mutex);
  970. if (n == 1) {
  971. ext2_free_data(inode, i_data+offsets[0],
  972. i_data + EXT2_NDIR_BLOCKS);
  973. goto do_indirects;
  974. }
  975. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  976. /* Kill the top of shared branch (already detached) */
  977. if (nr) {
  978. if (partial == chain)
  979. mark_inode_dirty(inode);
  980. else
  981. mark_buffer_dirty_inode(partial->bh, inode);
  982. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  983. }
  984. /* Clear the ends of indirect blocks on the shared branch */
  985. while (partial > chain) {
  986. ext2_free_branches(inode,
  987. partial->p + 1,
  988. (__le32*)partial->bh->b_data+addr_per_block,
  989. (chain+n-1) - partial);
  990. mark_buffer_dirty_inode(partial->bh, inode);
  991. brelse (partial->bh);
  992. partial--;
  993. }
  994. do_indirects:
  995. /* Kill the remaining (whole) subtrees */
  996. switch (offsets[0]) {
  997. default:
  998. nr = i_data[EXT2_IND_BLOCK];
  999. if (nr) {
  1000. i_data[EXT2_IND_BLOCK] = 0;
  1001. mark_inode_dirty(inode);
  1002. ext2_free_branches(inode, &nr, &nr+1, 1);
  1003. }
  1004. case EXT2_IND_BLOCK:
  1005. nr = i_data[EXT2_DIND_BLOCK];
  1006. if (nr) {
  1007. i_data[EXT2_DIND_BLOCK] = 0;
  1008. mark_inode_dirty(inode);
  1009. ext2_free_branches(inode, &nr, &nr+1, 2);
  1010. }
  1011. case EXT2_DIND_BLOCK:
  1012. nr = i_data[EXT2_TIND_BLOCK];
  1013. if (nr) {
  1014. i_data[EXT2_TIND_BLOCK] = 0;
  1015. mark_inode_dirty(inode);
  1016. ext2_free_branches(inode, &nr, &nr+1, 3);
  1017. }
  1018. case EXT2_TIND_BLOCK:
  1019. ;
  1020. }
  1021. ext2_discard_reservation(inode);
  1022. mutex_unlock(&ei->truncate_mutex);
  1023. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1024. if (inode_needs_sync(inode)) {
  1025. sync_mapping_buffers(inode->i_mapping);
  1026. ext2_sync_inode (inode);
  1027. } else {
  1028. mark_inode_dirty(inode);
  1029. }
  1030. }
  1031. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1032. struct buffer_head **p)
  1033. {
  1034. struct buffer_head * bh;
  1035. unsigned long block_group;
  1036. unsigned long block;
  1037. unsigned long offset;
  1038. struct ext2_group_desc * gdp;
  1039. *p = NULL;
  1040. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1041. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1042. goto Einval;
  1043. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1044. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1045. if (!gdp)
  1046. goto Egdp;
  1047. /*
  1048. * Figure out the offset within the block group inode table
  1049. */
  1050. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1051. block = le32_to_cpu(gdp->bg_inode_table) +
  1052. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1053. if (!(bh = sb_bread(sb, block)))
  1054. goto Eio;
  1055. *p = bh;
  1056. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1057. return (struct ext2_inode *) (bh->b_data + offset);
  1058. Einval:
  1059. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1060. (unsigned long) ino);
  1061. return ERR_PTR(-EINVAL);
  1062. Eio:
  1063. ext2_error(sb, "ext2_get_inode",
  1064. "unable to read inode block - inode=%lu, block=%lu",
  1065. (unsigned long) ino, block);
  1066. Egdp:
  1067. return ERR_PTR(-EIO);
  1068. }
  1069. void ext2_set_inode_flags(struct inode *inode)
  1070. {
  1071. unsigned int flags = EXT2_I(inode)->i_flags;
  1072. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1073. if (flags & EXT2_SYNC_FL)
  1074. inode->i_flags |= S_SYNC;
  1075. if (flags & EXT2_APPEND_FL)
  1076. inode->i_flags |= S_APPEND;
  1077. if (flags & EXT2_IMMUTABLE_FL)
  1078. inode->i_flags |= S_IMMUTABLE;
  1079. if (flags & EXT2_NOATIME_FL)
  1080. inode->i_flags |= S_NOATIME;
  1081. if (flags & EXT2_DIRSYNC_FL)
  1082. inode->i_flags |= S_DIRSYNC;
  1083. }
  1084. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1085. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1086. {
  1087. unsigned int flags = ei->vfs_inode.i_flags;
  1088. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1089. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1090. if (flags & S_SYNC)
  1091. ei->i_flags |= EXT2_SYNC_FL;
  1092. if (flags & S_APPEND)
  1093. ei->i_flags |= EXT2_APPEND_FL;
  1094. if (flags & S_IMMUTABLE)
  1095. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1096. if (flags & S_NOATIME)
  1097. ei->i_flags |= EXT2_NOATIME_FL;
  1098. if (flags & S_DIRSYNC)
  1099. ei->i_flags |= EXT2_DIRSYNC_FL;
  1100. }
  1101. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1102. {
  1103. struct ext2_inode_info *ei;
  1104. struct buffer_head * bh;
  1105. struct ext2_inode *raw_inode;
  1106. struct inode *inode;
  1107. long ret = -EIO;
  1108. int n;
  1109. inode = iget_locked(sb, ino);
  1110. if (!inode)
  1111. return ERR_PTR(-ENOMEM);
  1112. if (!(inode->i_state & I_NEW))
  1113. return inode;
  1114. ei = EXT2_I(inode);
  1115. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  1116. ei->i_acl = EXT2_ACL_NOT_CACHED;
  1117. ei->i_default_acl = EXT2_ACL_NOT_CACHED;
  1118. #endif
  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_update_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, int wait)
  1320. {
  1321. return ext2_update_inode(inode, wait);
  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. }