inode.c 44 KB

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