inode.c 45 KB

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