inode.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  558. if (depth == 0)
  559. return (err);
  560. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  561. /* Simplest case - block found, no allocation needed */
  562. if (!partial) {
  563. first_block = le32_to_cpu(chain[depth - 1].key);
  564. clear_buffer_new(bh_result); /* What's this do? */
  565. count++;
  566. /*map more blocks*/
  567. while (count < maxblocks && count <= blocks_to_boundary) {
  568. ext2_fsblk_t blk;
  569. if (!verify_chain(chain, chain + depth - 1)) {
  570. /*
  571. * Indirect block might be removed by
  572. * truncate while we were reading it.
  573. * Handling of that case: forget what we've
  574. * got now, go to reread.
  575. */
  576. err = -EAGAIN;
  577. count = 0;
  578. break;
  579. }
  580. blk = le32_to_cpu(*(chain[depth-1].p + count));
  581. if (blk == first_block + count)
  582. count++;
  583. else
  584. break;
  585. }
  586. if (err != -EAGAIN)
  587. goto got_it;
  588. }
  589. /* Next simple case - plain lookup or failed read of indirect block */
  590. if (!create || err == -EIO)
  591. goto cleanup;
  592. mutex_lock(&ei->truncate_mutex);
  593. /*
  594. * If the indirect block is missing while we are reading
  595. * the chain(ext2_get_branch() returns -EAGAIN err), or
  596. * if the chain has been changed after we grab the semaphore,
  597. * (either because another process truncated this branch, or
  598. * another get_block allocated this branch) re-grab the chain to see if
  599. * the request block has been allocated or not.
  600. *
  601. * Since we already block the truncate/other get_block
  602. * at this point, we will have the current copy of the chain when we
  603. * splice the branch into the tree.
  604. */
  605. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  606. while (partial > chain) {
  607. brelse(partial->bh);
  608. partial--;
  609. }
  610. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  611. if (!partial) {
  612. count++;
  613. mutex_unlock(&ei->truncate_mutex);
  614. if (err)
  615. goto cleanup;
  616. clear_buffer_new(bh_result);
  617. goto got_it;
  618. }
  619. }
  620. /*
  621. * Okay, we need to do block allocation. Lazily initialize the block
  622. * allocation info here if necessary
  623. */
  624. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  625. ext2_init_block_alloc_info(inode);
  626. goal = ext2_find_goal(inode, iblock, partial);
  627. /* the number of blocks need to allocate for [d,t]indirect blocks */
  628. indirect_blks = (chain + depth) - partial - 1;
  629. /*
  630. * Next look up the indirect map to count the totoal number of
  631. * direct blocks to allocate for this branch.
  632. */
  633. count = ext2_blks_to_allocate(partial, indirect_blks,
  634. maxblocks, blocks_to_boundary);
  635. /*
  636. * XXX ???? Block out ext2_truncate while we alter the tree
  637. */
  638. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  639. offsets + (partial - chain), partial);
  640. if (err) {
  641. mutex_unlock(&ei->truncate_mutex);
  642. goto cleanup;
  643. }
  644. if (ext2_use_xip(inode->i_sb)) {
  645. /*
  646. * we need to clear the block
  647. */
  648. err = ext2_clear_xip_target (inode,
  649. le32_to_cpu(chain[depth-1].key));
  650. if (err) {
  651. mutex_unlock(&ei->truncate_mutex);
  652. goto cleanup;
  653. }
  654. }
  655. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  656. mutex_unlock(&ei->truncate_mutex);
  657. set_buffer_new(bh_result);
  658. got_it:
  659. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  660. if (count > blocks_to_boundary)
  661. set_buffer_boundary(bh_result);
  662. err = count;
  663. /* Clean up and exit */
  664. partial = chain + depth - 1; /* the whole chain */
  665. cleanup:
  666. while (partial > chain) {
  667. brelse(partial->bh);
  668. partial--;
  669. }
  670. return err;
  671. }
  672. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  673. {
  674. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  675. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  676. bh_result, create);
  677. if (ret > 0) {
  678. bh_result->b_size = (ret << inode->i_blkbits);
  679. ret = 0;
  680. }
  681. return ret;
  682. }
  683. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  684. u64 start, u64 len)
  685. {
  686. return generic_block_fiemap(inode, fieinfo, start, len,
  687. ext2_get_block);
  688. }
  689. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  690. {
  691. return block_write_full_page(page, ext2_get_block, wbc);
  692. }
  693. static int ext2_readpage(struct file *file, struct page *page)
  694. {
  695. return mpage_readpage(page, ext2_get_block);
  696. }
  697. static int
  698. ext2_readpages(struct file *file, struct address_space *mapping,
  699. struct list_head *pages, unsigned nr_pages)
  700. {
  701. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  702. }
  703. static int
  704. ext2_write_begin(struct file *file, struct address_space *mapping,
  705. loff_t pos, unsigned len, unsigned flags,
  706. struct page **pagep, void **fsdata)
  707. {
  708. int ret;
  709. ret = block_write_begin(mapping, pos, len, flags, pagep,
  710. ext2_get_block);
  711. if (ret < 0)
  712. ext2_write_failed(mapping, pos + len);
  713. return ret;
  714. }
  715. static int ext2_write_end(struct file *file, struct address_space *mapping,
  716. loff_t pos, unsigned len, unsigned copied,
  717. struct page *page, void *fsdata)
  718. {
  719. int ret;
  720. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  721. if (ret < len)
  722. ext2_write_failed(mapping, pos + len);
  723. return ret;
  724. }
  725. static int
  726. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  727. loff_t pos, unsigned len, unsigned flags,
  728. struct page **pagep, void **fsdata)
  729. {
  730. int ret;
  731. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  732. ext2_get_block);
  733. if (ret < 0)
  734. ext2_write_failed(mapping, pos + len);
  735. return ret;
  736. }
  737. static int ext2_nobh_writepage(struct page *page,
  738. struct writeback_control *wbc)
  739. {
  740. return nobh_writepage(page, ext2_get_block, wbc);
  741. }
  742. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  743. {
  744. return generic_block_bmap(mapping,block,ext2_get_block);
  745. }
  746. static ssize_t
  747. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  748. loff_t offset, unsigned long nr_segs)
  749. {
  750. struct file *file = iocb->ki_filp;
  751. struct address_space *mapping = file->f_mapping;
  752. struct inode *inode = mapping->host;
  753. ssize_t ret;
  754. ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
  755. ext2_get_block);
  756. if (ret < 0 && (rw & WRITE))
  757. ext2_write_failed(mapping, offset + iov_length(iov, nr_segs));
  758. return ret;
  759. }
  760. static int
  761. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  762. {
  763. return mpage_writepages(mapping, wbc, ext2_get_block);
  764. }
  765. const struct address_space_operations ext2_aops = {
  766. .readpage = ext2_readpage,
  767. .readpages = ext2_readpages,
  768. .writepage = ext2_writepage,
  769. .write_begin = ext2_write_begin,
  770. .write_end = ext2_write_end,
  771. .bmap = ext2_bmap,
  772. .direct_IO = ext2_direct_IO,
  773. .writepages = ext2_writepages,
  774. .migratepage = buffer_migrate_page,
  775. .is_partially_uptodate = block_is_partially_uptodate,
  776. .error_remove_page = generic_error_remove_page,
  777. };
  778. const struct address_space_operations ext2_aops_xip = {
  779. .bmap = ext2_bmap,
  780. .get_xip_mem = ext2_get_xip_mem,
  781. };
  782. const struct address_space_operations ext2_nobh_aops = {
  783. .readpage = ext2_readpage,
  784. .readpages = ext2_readpages,
  785. .writepage = ext2_nobh_writepage,
  786. .write_begin = ext2_nobh_write_begin,
  787. .write_end = nobh_write_end,
  788. .bmap = ext2_bmap,
  789. .direct_IO = ext2_direct_IO,
  790. .writepages = ext2_writepages,
  791. .migratepage = buffer_migrate_page,
  792. .error_remove_page = generic_error_remove_page,
  793. };
  794. /*
  795. * Probably it should be a library function... search for first non-zero word
  796. * or memcmp with zero_page, whatever is better for particular architecture.
  797. * Linus?
  798. */
  799. static inline int all_zeroes(__le32 *p, __le32 *q)
  800. {
  801. while (p < q)
  802. if (*p++)
  803. return 0;
  804. return 1;
  805. }
  806. /**
  807. * ext2_find_shared - find the indirect blocks for partial truncation.
  808. * @inode: inode in question
  809. * @depth: depth of the affected branch
  810. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  811. * @chain: place to store the pointers to partial indirect blocks
  812. * @top: place to the (detached) top of branch
  813. *
  814. * This is a helper function used by ext2_truncate().
  815. *
  816. * When we do truncate() we may have to clean the ends of several indirect
  817. * blocks but leave the blocks themselves alive. Block is partially
  818. * truncated if some data below the new i_size is referred from it (and
  819. * it is on the path to the first completely truncated data block, indeed).
  820. * We have to free the top of that path along with everything to the right
  821. * of the path. Since no allocation past the truncation point is possible
  822. * until ext2_truncate() finishes, we may safely do the latter, but top
  823. * of branch may require special attention - pageout below the truncation
  824. * point might try to populate it.
  825. *
  826. * We atomically detach the top of branch from the tree, store the block
  827. * number of its root in *@top, pointers to buffer_heads of partially
  828. * truncated blocks - in @chain[].bh and pointers to their last elements
  829. * that should not be removed - in @chain[].p. Return value is the pointer
  830. * to last filled element of @chain.
  831. *
  832. * The work left to caller to do the actual freeing of subtrees:
  833. * a) free the subtree starting from *@top
  834. * b) free the subtrees whose roots are stored in
  835. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  836. * c) free the subtrees growing from the inode past the @chain[0].p
  837. * (no partially truncated stuff there).
  838. */
  839. static Indirect *ext2_find_shared(struct inode *inode,
  840. int depth,
  841. int offsets[4],
  842. Indirect chain[4],
  843. __le32 *top)
  844. {
  845. Indirect *partial, *p;
  846. int k, err;
  847. *top = 0;
  848. for (k = depth; k > 1 && !offsets[k-1]; k--)
  849. ;
  850. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  851. if (!partial)
  852. partial = chain + k-1;
  853. /*
  854. * If the branch acquired continuation since we've looked at it -
  855. * fine, it should all survive and (new) top doesn't belong to us.
  856. */
  857. write_lock(&EXT2_I(inode)->i_meta_lock);
  858. if (!partial->key && *partial->p) {
  859. write_unlock(&EXT2_I(inode)->i_meta_lock);
  860. goto no_top;
  861. }
  862. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  863. ;
  864. /*
  865. * OK, we've found the last block that must survive. The rest of our
  866. * branch should be detached before unlocking. However, if that rest
  867. * of branch is all ours and does not grow immediately from the inode
  868. * it's easier to cheat and just decrement partial->p.
  869. */
  870. if (p == chain + k - 1 && p > chain) {
  871. p->p--;
  872. } else {
  873. *top = *p->p;
  874. *p->p = 0;
  875. }
  876. write_unlock(&EXT2_I(inode)->i_meta_lock);
  877. while(partial > p)
  878. {
  879. brelse(partial->bh);
  880. partial--;
  881. }
  882. no_top:
  883. return partial;
  884. }
  885. /**
  886. * ext2_free_data - free a list of data blocks
  887. * @inode: inode we are dealing with
  888. * @p: array of block numbers
  889. * @q: points immediately past the end of array
  890. *
  891. * We are freeing all blocks referred from that array (numbers are
  892. * stored as little-endian 32-bit) and updating @inode->i_blocks
  893. * appropriately.
  894. */
  895. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  896. {
  897. unsigned long block_to_free = 0, count = 0;
  898. unsigned long nr;
  899. for ( ; p < q ; p++) {
  900. nr = le32_to_cpu(*p);
  901. if (nr) {
  902. *p = 0;
  903. /* accumulate blocks to free if they're contiguous */
  904. if (count == 0)
  905. goto free_this;
  906. else if (block_to_free == nr - count)
  907. count++;
  908. else {
  909. ext2_free_blocks (inode, block_to_free, count);
  910. mark_inode_dirty(inode);
  911. free_this:
  912. block_to_free = nr;
  913. count = 1;
  914. }
  915. }
  916. }
  917. if (count > 0) {
  918. ext2_free_blocks (inode, block_to_free, count);
  919. mark_inode_dirty(inode);
  920. }
  921. }
  922. /**
  923. * ext2_free_branches - free an array of branches
  924. * @inode: inode we are dealing with
  925. * @p: array of block numbers
  926. * @q: pointer immediately past the end of array
  927. * @depth: depth of the branches to free
  928. *
  929. * We are freeing all blocks referred from these branches (numbers are
  930. * stored as little-endian 32-bit) and updating @inode->i_blocks
  931. * appropriately.
  932. */
  933. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  934. {
  935. struct buffer_head * bh;
  936. unsigned long nr;
  937. if (depth--) {
  938. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  939. for ( ; p < q ; p++) {
  940. nr = le32_to_cpu(*p);
  941. if (!nr)
  942. continue;
  943. *p = 0;
  944. bh = sb_bread(inode->i_sb, nr);
  945. /*
  946. * A read failure? Report error and clear slot
  947. * (should be rare).
  948. */
  949. if (!bh) {
  950. ext2_error(inode->i_sb, "ext2_free_branches",
  951. "Read failure, inode=%ld, block=%ld",
  952. inode->i_ino, nr);
  953. continue;
  954. }
  955. ext2_free_branches(inode,
  956. (__le32*)bh->b_data,
  957. (__le32*)bh->b_data + addr_per_block,
  958. depth);
  959. bforget(bh);
  960. ext2_free_blocks(inode, nr, 1);
  961. mark_inode_dirty(inode);
  962. }
  963. } else
  964. ext2_free_data(inode, p, q);
  965. }
  966. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  967. {
  968. __le32 *i_data = EXT2_I(inode)->i_data;
  969. struct ext2_inode_info *ei = EXT2_I(inode);
  970. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  971. int offsets[4];
  972. Indirect chain[4];
  973. Indirect *partial;
  974. __le32 nr = 0;
  975. int n;
  976. long iblock;
  977. unsigned blocksize;
  978. blocksize = inode->i_sb->s_blocksize;
  979. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  980. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  981. if (n == 0)
  982. return;
  983. /*
  984. * From here we block out all ext2_get_block() callers who want to
  985. * modify the block allocation tree.
  986. */
  987. mutex_lock(&ei->truncate_mutex);
  988. if (n == 1) {
  989. ext2_free_data(inode, i_data+offsets[0],
  990. i_data + EXT2_NDIR_BLOCKS);
  991. goto do_indirects;
  992. }
  993. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  994. /* Kill the top of shared branch (already detached) */
  995. if (nr) {
  996. if (partial == chain)
  997. mark_inode_dirty(inode);
  998. else
  999. mark_buffer_dirty_inode(partial->bh, inode);
  1000. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1001. }
  1002. /* Clear the ends of indirect blocks on the shared branch */
  1003. while (partial > chain) {
  1004. ext2_free_branches(inode,
  1005. partial->p + 1,
  1006. (__le32*)partial->bh->b_data+addr_per_block,
  1007. (chain+n-1) - partial);
  1008. mark_buffer_dirty_inode(partial->bh, inode);
  1009. brelse (partial->bh);
  1010. partial--;
  1011. }
  1012. do_indirects:
  1013. /* Kill the remaining (whole) subtrees */
  1014. switch (offsets[0]) {
  1015. default:
  1016. nr = i_data[EXT2_IND_BLOCK];
  1017. if (nr) {
  1018. i_data[EXT2_IND_BLOCK] = 0;
  1019. mark_inode_dirty(inode);
  1020. ext2_free_branches(inode, &nr, &nr+1, 1);
  1021. }
  1022. case EXT2_IND_BLOCK:
  1023. nr = i_data[EXT2_DIND_BLOCK];
  1024. if (nr) {
  1025. i_data[EXT2_DIND_BLOCK] = 0;
  1026. mark_inode_dirty(inode);
  1027. ext2_free_branches(inode, &nr, &nr+1, 2);
  1028. }
  1029. case EXT2_DIND_BLOCK:
  1030. nr = i_data[EXT2_TIND_BLOCK];
  1031. if (nr) {
  1032. i_data[EXT2_TIND_BLOCK] = 0;
  1033. mark_inode_dirty(inode);
  1034. ext2_free_branches(inode, &nr, &nr+1, 3);
  1035. }
  1036. case EXT2_TIND_BLOCK:
  1037. ;
  1038. }
  1039. ext2_discard_reservation(inode);
  1040. mutex_unlock(&ei->truncate_mutex);
  1041. }
  1042. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1043. {
  1044. /*
  1045. * XXX: it seems like a bug here that we don't allow
  1046. * IS_APPEND inode to have blocks-past-i_size trimmed off.
  1047. * review and fix this.
  1048. *
  1049. * Also would be nice to be able to handle IO errors and such,
  1050. * but that's probably too much to ask.
  1051. */
  1052. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1053. S_ISLNK(inode->i_mode)))
  1054. return;
  1055. if (ext2_inode_is_fast_symlink(inode))
  1056. return;
  1057. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1058. return;
  1059. __ext2_truncate_blocks(inode, offset);
  1060. }
  1061. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1062. {
  1063. int error;
  1064. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1065. S_ISLNK(inode->i_mode)))
  1066. return -EINVAL;
  1067. if (ext2_inode_is_fast_symlink(inode))
  1068. return -EINVAL;
  1069. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1070. return -EPERM;
  1071. inode_dio_wait(inode);
  1072. if (mapping_is_xip(inode->i_mapping))
  1073. error = xip_truncate_page(inode->i_mapping, newsize);
  1074. else if (test_opt(inode->i_sb, NOBH))
  1075. error = nobh_truncate_page(inode->i_mapping,
  1076. newsize, ext2_get_block);
  1077. else
  1078. error = block_truncate_page(inode->i_mapping,
  1079. newsize, ext2_get_block);
  1080. if (error)
  1081. return error;
  1082. truncate_setsize(inode, newsize);
  1083. __ext2_truncate_blocks(inode, newsize);
  1084. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1085. if (inode_needs_sync(inode)) {
  1086. sync_mapping_buffers(inode->i_mapping);
  1087. sync_inode_metadata(inode, 1);
  1088. } else {
  1089. mark_inode_dirty(inode);
  1090. }
  1091. return 0;
  1092. }
  1093. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1094. struct buffer_head **p)
  1095. {
  1096. struct buffer_head * bh;
  1097. unsigned long block_group;
  1098. unsigned long block;
  1099. unsigned long offset;
  1100. struct ext2_group_desc * gdp;
  1101. *p = NULL;
  1102. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1103. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1104. goto Einval;
  1105. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1106. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1107. if (!gdp)
  1108. goto Egdp;
  1109. /*
  1110. * Figure out the offset within the block group inode table
  1111. */
  1112. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1113. block = le32_to_cpu(gdp->bg_inode_table) +
  1114. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1115. if (!(bh = sb_bread(sb, block)))
  1116. goto Eio;
  1117. *p = bh;
  1118. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1119. return (struct ext2_inode *) (bh->b_data + offset);
  1120. Einval:
  1121. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1122. (unsigned long) ino);
  1123. return ERR_PTR(-EINVAL);
  1124. Eio:
  1125. ext2_error(sb, "ext2_get_inode",
  1126. "unable to read inode block - inode=%lu, block=%lu",
  1127. (unsigned long) ino, block);
  1128. Egdp:
  1129. return ERR_PTR(-EIO);
  1130. }
  1131. void ext2_set_inode_flags(struct inode *inode)
  1132. {
  1133. unsigned int flags = EXT2_I(inode)->i_flags;
  1134. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1135. if (flags & EXT2_SYNC_FL)
  1136. inode->i_flags |= S_SYNC;
  1137. if (flags & EXT2_APPEND_FL)
  1138. inode->i_flags |= S_APPEND;
  1139. if (flags & EXT2_IMMUTABLE_FL)
  1140. inode->i_flags |= S_IMMUTABLE;
  1141. if (flags & EXT2_NOATIME_FL)
  1142. inode->i_flags |= S_NOATIME;
  1143. if (flags & EXT2_DIRSYNC_FL)
  1144. inode->i_flags |= S_DIRSYNC;
  1145. }
  1146. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1147. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1148. {
  1149. unsigned int flags = ei->vfs_inode.i_flags;
  1150. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1151. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1152. if (flags & S_SYNC)
  1153. ei->i_flags |= EXT2_SYNC_FL;
  1154. if (flags & S_APPEND)
  1155. ei->i_flags |= EXT2_APPEND_FL;
  1156. if (flags & S_IMMUTABLE)
  1157. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1158. if (flags & S_NOATIME)
  1159. ei->i_flags |= EXT2_NOATIME_FL;
  1160. if (flags & S_DIRSYNC)
  1161. ei->i_flags |= EXT2_DIRSYNC_FL;
  1162. }
  1163. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1164. {
  1165. struct ext2_inode_info *ei;
  1166. struct buffer_head * bh;
  1167. struct ext2_inode *raw_inode;
  1168. struct inode *inode;
  1169. long ret = -EIO;
  1170. int n;
  1171. inode = iget_locked(sb, ino);
  1172. if (!inode)
  1173. return ERR_PTR(-ENOMEM);
  1174. if (!(inode->i_state & I_NEW))
  1175. return inode;
  1176. ei = EXT2_I(inode);
  1177. ei->i_block_alloc_info = NULL;
  1178. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1179. if (IS_ERR(raw_inode)) {
  1180. ret = PTR_ERR(raw_inode);
  1181. goto bad_inode;
  1182. }
  1183. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1184. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1185. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1186. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1187. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1188. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1189. }
  1190. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1191. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1192. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1193. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1194. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1195. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1196. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1197. /* We now have enough fields to check if the inode was active or not.
  1198. * This is needed because nfsd might try to access dead inodes
  1199. * the test is that same one that e2fsck uses
  1200. * NeilBrown 1999oct15
  1201. */
  1202. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1203. /* this inode is deleted */
  1204. brelse (bh);
  1205. ret = -ESTALE;
  1206. goto bad_inode;
  1207. }
  1208. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1209. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1210. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1211. ei->i_frag_no = raw_inode->i_frag;
  1212. ei->i_frag_size = raw_inode->i_fsize;
  1213. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1214. ei->i_dir_acl = 0;
  1215. if (S_ISREG(inode->i_mode))
  1216. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1217. else
  1218. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1219. ei->i_dtime = 0;
  1220. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1221. ei->i_state = 0;
  1222. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1223. ei->i_dir_start_lookup = 0;
  1224. /*
  1225. * NOTE! The in-memory inode i_data array is in little-endian order
  1226. * even on big-endian machines: we do NOT byteswap the block numbers!
  1227. */
  1228. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1229. ei->i_data[n] = raw_inode->i_block[n];
  1230. if (S_ISREG(inode->i_mode)) {
  1231. inode->i_op = &ext2_file_inode_operations;
  1232. if (ext2_use_xip(inode->i_sb)) {
  1233. inode->i_mapping->a_ops = &ext2_aops_xip;
  1234. inode->i_fop = &ext2_xip_file_operations;
  1235. } else if (test_opt(inode->i_sb, NOBH)) {
  1236. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1237. inode->i_fop = &ext2_file_operations;
  1238. } else {
  1239. inode->i_mapping->a_ops = &ext2_aops;
  1240. inode->i_fop = &ext2_file_operations;
  1241. }
  1242. } else if (S_ISDIR(inode->i_mode)) {
  1243. inode->i_op = &ext2_dir_inode_operations;
  1244. inode->i_fop = &ext2_dir_operations;
  1245. if (test_opt(inode->i_sb, NOBH))
  1246. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1247. else
  1248. inode->i_mapping->a_ops = &ext2_aops;
  1249. } else if (S_ISLNK(inode->i_mode)) {
  1250. if (ext2_inode_is_fast_symlink(inode)) {
  1251. inode->i_op = &ext2_fast_symlink_inode_operations;
  1252. nd_terminate_link(ei->i_data, inode->i_size,
  1253. sizeof(ei->i_data) - 1);
  1254. } else {
  1255. inode->i_op = &ext2_symlink_inode_operations;
  1256. if (test_opt(inode->i_sb, NOBH))
  1257. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1258. else
  1259. inode->i_mapping->a_ops = &ext2_aops;
  1260. }
  1261. } else {
  1262. inode->i_op = &ext2_special_inode_operations;
  1263. if (raw_inode->i_block[0])
  1264. init_special_inode(inode, inode->i_mode,
  1265. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1266. else
  1267. init_special_inode(inode, inode->i_mode,
  1268. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1269. }
  1270. brelse (bh);
  1271. ext2_set_inode_flags(inode);
  1272. unlock_new_inode(inode);
  1273. return inode;
  1274. bad_inode:
  1275. iget_failed(inode);
  1276. return ERR_PTR(ret);
  1277. }
  1278. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1279. {
  1280. struct ext2_inode_info *ei = EXT2_I(inode);
  1281. struct super_block *sb = inode->i_sb;
  1282. ino_t ino = inode->i_ino;
  1283. uid_t uid = inode->i_uid;
  1284. gid_t gid = inode->i_gid;
  1285. struct buffer_head * bh;
  1286. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1287. int n;
  1288. int err = 0;
  1289. if (IS_ERR(raw_inode))
  1290. return -EIO;
  1291. /* For fields not not tracking in the in-memory inode,
  1292. * initialise them to zero for new inodes. */
  1293. if (ei->i_state & EXT2_STATE_NEW)
  1294. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1295. ext2_get_inode_flags(ei);
  1296. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1297. if (!(test_opt(sb, NO_UID32))) {
  1298. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1299. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1300. /*
  1301. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1302. * re-used with the upper 16 bits of the uid/gid intact
  1303. */
  1304. if (!ei->i_dtime) {
  1305. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1306. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1307. } else {
  1308. raw_inode->i_uid_high = 0;
  1309. raw_inode->i_gid_high = 0;
  1310. }
  1311. } else {
  1312. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1313. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1314. raw_inode->i_uid_high = 0;
  1315. raw_inode->i_gid_high = 0;
  1316. }
  1317. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1318. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1319. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1320. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1321. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1322. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1323. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1324. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1325. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1326. raw_inode->i_frag = ei->i_frag_no;
  1327. raw_inode->i_fsize = ei->i_frag_size;
  1328. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1329. if (!S_ISREG(inode->i_mode))
  1330. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1331. else {
  1332. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1333. if (inode->i_size > 0x7fffffffULL) {
  1334. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1335. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1336. EXT2_SB(sb)->s_es->s_rev_level ==
  1337. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1338. /* If this is the first large file
  1339. * created, add a flag to the superblock.
  1340. */
  1341. spin_lock(&EXT2_SB(sb)->s_lock);
  1342. ext2_update_dynamic_rev(sb);
  1343. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1344. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1345. spin_unlock(&EXT2_SB(sb)->s_lock);
  1346. ext2_write_super(sb);
  1347. }
  1348. }
  1349. }
  1350. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1351. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1352. if (old_valid_dev(inode->i_rdev)) {
  1353. raw_inode->i_block[0] =
  1354. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1355. raw_inode->i_block[1] = 0;
  1356. } else {
  1357. raw_inode->i_block[0] = 0;
  1358. raw_inode->i_block[1] =
  1359. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1360. raw_inode->i_block[2] = 0;
  1361. }
  1362. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1363. raw_inode->i_block[n] = ei->i_data[n];
  1364. mark_buffer_dirty(bh);
  1365. if (do_sync) {
  1366. sync_dirty_buffer(bh);
  1367. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1368. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1369. sb->s_id, (unsigned long) ino);
  1370. err = -EIO;
  1371. }
  1372. }
  1373. ei->i_state &= ~EXT2_STATE_NEW;
  1374. brelse (bh);
  1375. return err;
  1376. }
  1377. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1378. {
  1379. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1380. }
  1381. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1382. {
  1383. struct inode *inode = dentry->d_inode;
  1384. int error;
  1385. error = inode_change_ok(inode, iattr);
  1386. if (error)
  1387. return error;
  1388. if (is_quota_modification(inode, iattr))
  1389. dquot_initialize(inode);
  1390. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  1391. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  1392. error = dquot_transfer(inode, iattr);
  1393. if (error)
  1394. return error;
  1395. }
  1396. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1397. error = ext2_setsize(inode, iattr->ia_size);
  1398. if (error)
  1399. return error;
  1400. }
  1401. setattr_copy(inode, iattr);
  1402. if (iattr->ia_valid & ATTR_MODE)
  1403. error = ext2_acl_chmod(inode);
  1404. mark_inode_dirty(inode);
  1405. return error;
  1406. }