inode.c 43 KB

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