inode.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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/dax.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/writeback.h>
  30. #include <linux/buffer_head.h>
  31. #include <linux/mpage.h>
  32. #include <linux/fiemap.h>
  33. #include <linux/namei.h>
  34. #include <linux/uio.h>
  35. #include "ext2.h"
  36. #include "acl.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_final(&inode->i_data);
  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 (IS_DAX(inode)) {
  663. /*
  664. * block must be initialised before we put it in the tree
  665. * so that it's not found by another thread before it's
  666. * initialised
  667. */
  668. err = dax_clear_blocks(inode, le32_to_cpu(chain[depth-1].key),
  669. 1 << inode->i_blkbits);
  670. if (err) {
  671. mutex_unlock(&ei->truncate_mutex);
  672. goto cleanup;
  673. }
  674. }
  675. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  676. mutex_unlock(&ei->truncate_mutex);
  677. set_buffer_new(bh_result);
  678. got_it:
  679. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  680. if (count > blocks_to_boundary)
  681. set_buffer_boundary(bh_result);
  682. err = count;
  683. /* Clean up and exit */
  684. partial = chain + depth - 1; /* the whole chain */
  685. cleanup:
  686. while (partial > chain) {
  687. brelse(partial->bh);
  688. partial--;
  689. }
  690. return err;
  691. }
  692. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  693. {
  694. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  695. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  696. bh_result, create);
  697. if (ret > 0) {
  698. bh_result->b_size = (ret << inode->i_blkbits);
  699. ret = 0;
  700. }
  701. return ret;
  702. }
  703. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  704. u64 start, u64 len)
  705. {
  706. return generic_block_fiemap(inode, fieinfo, start, len,
  707. ext2_get_block);
  708. }
  709. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  710. {
  711. return block_write_full_page(page, ext2_get_block, wbc);
  712. }
  713. static int ext2_readpage(struct file *file, struct page *page)
  714. {
  715. return mpage_readpage(page, ext2_get_block);
  716. }
  717. static int
  718. ext2_readpages(struct file *file, struct address_space *mapping,
  719. struct list_head *pages, unsigned nr_pages)
  720. {
  721. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  722. }
  723. static int
  724. ext2_write_begin(struct file *file, struct address_space *mapping,
  725. loff_t pos, unsigned len, unsigned flags,
  726. struct page **pagep, void **fsdata)
  727. {
  728. int ret;
  729. ret = block_write_begin(mapping, pos, len, flags, pagep,
  730. ext2_get_block);
  731. if (ret < 0)
  732. ext2_write_failed(mapping, pos + len);
  733. return ret;
  734. }
  735. static int ext2_write_end(struct file *file, struct address_space *mapping,
  736. loff_t pos, unsigned len, unsigned copied,
  737. struct page *page, void *fsdata)
  738. {
  739. int ret;
  740. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  741. if (ret < len)
  742. ext2_write_failed(mapping, pos + len);
  743. return ret;
  744. }
  745. static int
  746. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  747. loff_t pos, unsigned len, unsigned flags,
  748. struct page **pagep, void **fsdata)
  749. {
  750. int ret;
  751. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  752. ext2_get_block);
  753. if (ret < 0)
  754. ext2_write_failed(mapping, pos + len);
  755. return ret;
  756. }
  757. static int ext2_nobh_writepage(struct page *page,
  758. struct writeback_control *wbc)
  759. {
  760. return nobh_writepage(page, ext2_get_block, wbc);
  761. }
  762. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  763. {
  764. return generic_block_bmap(mapping,block,ext2_get_block);
  765. }
  766. static ssize_t
  767. ext2_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
  768. {
  769. struct file *file = iocb->ki_filp;
  770. struct address_space *mapping = file->f_mapping;
  771. struct inode *inode = mapping->host;
  772. size_t count = iov_iter_count(iter);
  773. ssize_t ret;
  774. if (IS_DAX(inode))
  775. ret = dax_do_io(iocb, inode, iter, offset, ext2_get_block, NULL,
  776. DIO_LOCKING);
  777. else
  778. ret = blockdev_direct_IO(iocb, inode, iter, offset,
  779. ext2_get_block);
  780. if (ret < 0 && iov_iter_rw(iter) == WRITE)
  781. ext2_write_failed(mapping, offset + count);
  782. return ret;
  783. }
  784. static int
  785. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  786. {
  787. return mpage_writepages(mapping, wbc, ext2_get_block);
  788. }
  789. const struct address_space_operations ext2_aops = {
  790. .readpage = ext2_readpage,
  791. .readpages = ext2_readpages,
  792. .writepage = ext2_writepage,
  793. .write_begin = ext2_write_begin,
  794. .write_end = ext2_write_end,
  795. .bmap = ext2_bmap,
  796. .direct_IO = ext2_direct_IO,
  797. .writepages = ext2_writepages,
  798. .migratepage = buffer_migrate_page,
  799. .is_partially_uptodate = block_is_partially_uptodate,
  800. .error_remove_page = generic_error_remove_page,
  801. };
  802. const struct address_space_operations ext2_nobh_aops = {
  803. .readpage = ext2_readpage,
  804. .readpages = ext2_readpages,
  805. .writepage = ext2_nobh_writepage,
  806. .write_begin = ext2_nobh_write_begin,
  807. .write_end = nobh_write_end,
  808. .bmap = ext2_bmap,
  809. .direct_IO = ext2_direct_IO,
  810. .writepages = ext2_writepages,
  811. .migratepage = buffer_migrate_page,
  812. .error_remove_page = generic_error_remove_page,
  813. };
  814. /*
  815. * Probably it should be a library function... search for first non-zero word
  816. * or memcmp with zero_page, whatever is better for particular architecture.
  817. * Linus?
  818. */
  819. static inline int all_zeroes(__le32 *p, __le32 *q)
  820. {
  821. while (p < q)
  822. if (*p++)
  823. return 0;
  824. return 1;
  825. }
  826. /**
  827. * ext2_find_shared - find the indirect blocks for partial truncation.
  828. * @inode: inode in question
  829. * @depth: depth of the affected branch
  830. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  831. * @chain: place to store the pointers to partial indirect blocks
  832. * @top: place to the (detached) top of branch
  833. *
  834. * This is a helper function used by ext2_truncate().
  835. *
  836. * When we do truncate() we may have to clean the ends of several indirect
  837. * blocks but leave the blocks themselves alive. Block is partially
  838. * truncated if some data below the new i_size is referred from it (and
  839. * it is on the path to the first completely truncated data block, indeed).
  840. * We have to free the top of that path along with everything to the right
  841. * of the path. Since no allocation past the truncation point is possible
  842. * until ext2_truncate() finishes, we may safely do the latter, but top
  843. * of branch may require special attention - pageout below the truncation
  844. * point might try to populate it.
  845. *
  846. * We atomically detach the top of branch from the tree, store the block
  847. * number of its root in *@top, pointers to buffer_heads of partially
  848. * truncated blocks - in @chain[].bh and pointers to their last elements
  849. * that should not be removed - in @chain[].p. Return value is the pointer
  850. * to last filled element of @chain.
  851. *
  852. * The work left to caller to do the actual freeing of subtrees:
  853. * a) free the subtree starting from *@top
  854. * b) free the subtrees whose roots are stored in
  855. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  856. * c) free the subtrees growing from the inode past the @chain[0].p
  857. * (no partially truncated stuff there).
  858. */
  859. static Indirect *ext2_find_shared(struct inode *inode,
  860. int depth,
  861. int offsets[4],
  862. Indirect chain[4],
  863. __le32 *top)
  864. {
  865. Indirect *partial, *p;
  866. int k, err;
  867. *top = 0;
  868. for (k = depth; k > 1 && !offsets[k-1]; k--)
  869. ;
  870. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  871. if (!partial)
  872. partial = chain + k-1;
  873. /*
  874. * If the branch acquired continuation since we've looked at it -
  875. * fine, it should all survive and (new) top doesn't belong to us.
  876. */
  877. write_lock(&EXT2_I(inode)->i_meta_lock);
  878. if (!partial->key && *partial->p) {
  879. write_unlock(&EXT2_I(inode)->i_meta_lock);
  880. goto no_top;
  881. }
  882. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  883. ;
  884. /*
  885. * OK, we've found the last block that must survive. The rest of our
  886. * branch should be detached before unlocking. However, if that rest
  887. * of branch is all ours and does not grow immediately from the inode
  888. * it's easier to cheat and just decrement partial->p.
  889. */
  890. if (p == chain + k - 1 && p > chain) {
  891. p->p--;
  892. } else {
  893. *top = *p->p;
  894. *p->p = 0;
  895. }
  896. write_unlock(&EXT2_I(inode)->i_meta_lock);
  897. while(partial > p)
  898. {
  899. brelse(partial->bh);
  900. partial--;
  901. }
  902. no_top:
  903. return partial;
  904. }
  905. /**
  906. * ext2_free_data - free a list of data blocks
  907. * @inode: inode we are dealing with
  908. * @p: array of block numbers
  909. * @q: points immediately past the end of array
  910. *
  911. * We are freeing all blocks referred from that array (numbers are
  912. * stored as little-endian 32-bit) and updating @inode->i_blocks
  913. * appropriately.
  914. */
  915. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  916. {
  917. unsigned long block_to_free = 0, count = 0;
  918. unsigned long nr;
  919. for ( ; p < q ; p++) {
  920. nr = le32_to_cpu(*p);
  921. if (nr) {
  922. *p = 0;
  923. /* accumulate blocks to free if they're contiguous */
  924. if (count == 0)
  925. goto free_this;
  926. else if (block_to_free == nr - count)
  927. count++;
  928. else {
  929. ext2_free_blocks (inode, block_to_free, count);
  930. mark_inode_dirty(inode);
  931. free_this:
  932. block_to_free = nr;
  933. count = 1;
  934. }
  935. }
  936. }
  937. if (count > 0) {
  938. ext2_free_blocks (inode, block_to_free, count);
  939. mark_inode_dirty(inode);
  940. }
  941. }
  942. /**
  943. * ext2_free_branches - free an array of branches
  944. * @inode: inode we are dealing with
  945. * @p: array of block numbers
  946. * @q: pointer immediately past the end of array
  947. * @depth: depth of the branches to free
  948. *
  949. * We are freeing all blocks referred from these branches (numbers are
  950. * stored as little-endian 32-bit) and updating @inode->i_blocks
  951. * appropriately.
  952. */
  953. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  954. {
  955. struct buffer_head * bh;
  956. unsigned long nr;
  957. if (depth--) {
  958. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  959. for ( ; p < q ; p++) {
  960. nr = le32_to_cpu(*p);
  961. if (!nr)
  962. continue;
  963. *p = 0;
  964. bh = sb_bread(inode->i_sb, nr);
  965. /*
  966. * A read failure? Report error and clear slot
  967. * (should be rare).
  968. */
  969. if (!bh) {
  970. ext2_error(inode->i_sb, "ext2_free_branches",
  971. "Read failure, inode=%ld, block=%ld",
  972. inode->i_ino, nr);
  973. continue;
  974. }
  975. ext2_free_branches(inode,
  976. (__le32*)bh->b_data,
  977. (__le32*)bh->b_data + addr_per_block,
  978. depth);
  979. bforget(bh);
  980. ext2_free_blocks(inode, nr, 1);
  981. mark_inode_dirty(inode);
  982. }
  983. } else
  984. ext2_free_data(inode, p, q);
  985. }
  986. /* dax_sem must be held when calling this function */
  987. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  988. {
  989. __le32 *i_data = EXT2_I(inode)->i_data;
  990. struct ext2_inode_info *ei = EXT2_I(inode);
  991. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  992. int offsets[4];
  993. Indirect chain[4];
  994. Indirect *partial;
  995. __le32 nr = 0;
  996. int n;
  997. long iblock;
  998. unsigned blocksize;
  999. blocksize = inode->i_sb->s_blocksize;
  1000. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  1001. #ifdef CONFIG_FS_DAX
  1002. WARN_ON(!rwsem_is_locked(&ei->dax_sem));
  1003. #endif
  1004. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  1005. if (n == 0)
  1006. return;
  1007. /*
  1008. * From here we block out all ext2_get_block() callers who want to
  1009. * modify the block allocation tree.
  1010. */
  1011. mutex_lock(&ei->truncate_mutex);
  1012. if (n == 1) {
  1013. ext2_free_data(inode, i_data+offsets[0],
  1014. i_data + EXT2_NDIR_BLOCKS);
  1015. goto do_indirects;
  1016. }
  1017. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  1018. /* Kill the top of shared branch (already detached) */
  1019. if (nr) {
  1020. if (partial == chain)
  1021. mark_inode_dirty(inode);
  1022. else
  1023. mark_buffer_dirty_inode(partial->bh, inode);
  1024. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1025. }
  1026. /* Clear the ends of indirect blocks on the shared branch */
  1027. while (partial > chain) {
  1028. ext2_free_branches(inode,
  1029. partial->p + 1,
  1030. (__le32*)partial->bh->b_data+addr_per_block,
  1031. (chain+n-1) - partial);
  1032. mark_buffer_dirty_inode(partial->bh, inode);
  1033. brelse (partial->bh);
  1034. partial--;
  1035. }
  1036. do_indirects:
  1037. /* Kill the remaining (whole) subtrees */
  1038. switch (offsets[0]) {
  1039. default:
  1040. nr = i_data[EXT2_IND_BLOCK];
  1041. if (nr) {
  1042. i_data[EXT2_IND_BLOCK] = 0;
  1043. mark_inode_dirty(inode);
  1044. ext2_free_branches(inode, &nr, &nr+1, 1);
  1045. }
  1046. case EXT2_IND_BLOCK:
  1047. nr = i_data[EXT2_DIND_BLOCK];
  1048. if (nr) {
  1049. i_data[EXT2_DIND_BLOCK] = 0;
  1050. mark_inode_dirty(inode);
  1051. ext2_free_branches(inode, &nr, &nr+1, 2);
  1052. }
  1053. case EXT2_DIND_BLOCK:
  1054. nr = i_data[EXT2_TIND_BLOCK];
  1055. if (nr) {
  1056. i_data[EXT2_TIND_BLOCK] = 0;
  1057. mark_inode_dirty(inode);
  1058. ext2_free_branches(inode, &nr, &nr+1, 3);
  1059. }
  1060. case EXT2_TIND_BLOCK:
  1061. ;
  1062. }
  1063. ext2_discard_reservation(inode);
  1064. mutex_unlock(&ei->truncate_mutex);
  1065. }
  1066. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1067. {
  1068. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1069. S_ISLNK(inode->i_mode)))
  1070. return;
  1071. if (ext2_inode_is_fast_symlink(inode))
  1072. return;
  1073. dax_sem_down_write(EXT2_I(inode));
  1074. __ext2_truncate_blocks(inode, offset);
  1075. dax_sem_up_write(EXT2_I(inode));
  1076. }
  1077. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1078. {
  1079. int error;
  1080. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1081. S_ISLNK(inode->i_mode)))
  1082. return -EINVAL;
  1083. if (ext2_inode_is_fast_symlink(inode))
  1084. return -EINVAL;
  1085. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1086. return -EPERM;
  1087. inode_dio_wait(inode);
  1088. if (IS_DAX(inode))
  1089. error = dax_truncate_page(inode, newsize, ext2_get_block);
  1090. else if (test_opt(inode->i_sb, NOBH))
  1091. error = nobh_truncate_page(inode->i_mapping,
  1092. newsize, ext2_get_block);
  1093. else
  1094. error = block_truncate_page(inode->i_mapping,
  1095. newsize, ext2_get_block);
  1096. if (error)
  1097. return error;
  1098. dax_sem_down_write(EXT2_I(inode));
  1099. truncate_setsize(inode, newsize);
  1100. __ext2_truncate_blocks(inode, newsize);
  1101. dax_sem_up_write(EXT2_I(inode));
  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 |
  1153. S_DIRSYNC | S_DAX);
  1154. if (flags & EXT2_SYNC_FL)
  1155. inode->i_flags |= S_SYNC;
  1156. if (flags & EXT2_APPEND_FL)
  1157. inode->i_flags |= S_APPEND;
  1158. if (flags & EXT2_IMMUTABLE_FL)
  1159. inode->i_flags |= S_IMMUTABLE;
  1160. if (flags & EXT2_NOATIME_FL)
  1161. inode->i_flags |= S_NOATIME;
  1162. if (flags & EXT2_DIRSYNC_FL)
  1163. inode->i_flags |= S_DIRSYNC;
  1164. if (test_opt(inode->i_sb, DAX))
  1165. inode->i_flags |= S_DAX;
  1166. }
  1167. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1168. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1169. {
  1170. unsigned int flags = ei->vfs_inode.i_flags;
  1171. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1172. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1173. if (flags & S_SYNC)
  1174. ei->i_flags |= EXT2_SYNC_FL;
  1175. if (flags & S_APPEND)
  1176. ei->i_flags |= EXT2_APPEND_FL;
  1177. if (flags & S_IMMUTABLE)
  1178. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1179. if (flags & S_NOATIME)
  1180. ei->i_flags |= EXT2_NOATIME_FL;
  1181. if (flags & S_DIRSYNC)
  1182. ei->i_flags |= EXT2_DIRSYNC_FL;
  1183. }
  1184. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1185. {
  1186. struct ext2_inode_info *ei;
  1187. struct buffer_head * bh;
  1188. struct ext2_inode *raw_inode;
  1189. struct inode *inode;
  1190. long ret = -EIO;
  1191. int n;
  1192. uid_t i_uid;
  1193. gid_t i_gid;
  1194. inode = iget_locked(sb, ino);
  1195. if (!inode)
  1196. return ERR_PTR(-ENOMEM);
  1197. if (!(inode->i_state & I_NEW))
  1198. return inode;
  1199. ei = EXT2_I(inode);
  1200. ei->i_block_alloc_info = NULL;
  1201. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1202. if (IS_ERR(raw_inode)) {
  1203. ret = PTR_ERR(raw_inode);
  1204. goto bad_inode;
  1205. }
  1206. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1207. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1208. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1209. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1210. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1211. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1212. }
  1213. i_uid_write(inode, i_uid);
  1214. i_gid_write(inode, i_gid);
  1215. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1216. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1217. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1218. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1219. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1220. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1221. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1222. /* We now have enough fields to check if the inode was active or not.
  1223. * This is needed because nfsd might try to access dead inodes
  1224. * the test is that same one that e2fsck uses
  1225. * NeilBrown 1999oct15
  1226. */
  1227. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1228. /* this inode is deleted */
  1229. brelse (bh);
  1230. ret = -ESTALE;
  1231. goto bad_inode;
  1232. }
  1233. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1234. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1235. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1236. ei->i_frag_no = raw_inode->i_frag;
  1237. ei->i_frag_size = raw_inode->i_fsize;
  1238. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1239. ei->i_dir_acl = 0;
  1240. if (S_ISREG(inode->i_mode))
  1241. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1242. else
  1243. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1244. ei->i_dtime = 0;
  1245. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1246. ei->i_state = 0;
  1247. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1248. ei->i_dir_start_lookup = 0;
  1249. /*
  1250. * NOTE! The in-memory inode i_data array is in little-endian order
  1251. * even on big-endian machines: we do NOT byteswap the block numbers!
  1252. */
  1253. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1254. ei->i_data[n] = raw_inode->i_block[n];
  1255. if (S_ISREG(inode->i_mode)) {
  1256. inode->i_op = &ext2_file_inode_operations;
  1257. 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_link = (char *)ei->i_data;
  1274. inode->i_op = &ext2_fast_symlink_inode_operations;
  1275. nd_terminate_link(ei->i_data, inode->i_size,
  1276. sizeof(ei->i_data) - 1);
  1277. } else {
  1278. inode->i_op = &ext2_symlink_inode_operations;
  1279. if (test_opt(inode->i_sb, NOBH))
  1280. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1281. else
  1282. inode->i_mapping->a_ops = &ext2_aops;
  1283. }
  1284. } else {
  1285. inode->i_op = &ext2_special_inode_operations;
  1286. if (raw_inode->i_block[0])
  1287. init_special_inode(inode, inode->i_mode,
  1288. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1289. else
  1290. init_special_inode(inode, inode->i_mode,
  1291. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1292. }
  1293. brelse (bh);
  1294. ext2_set_inode_flags(inode);
  1295. unlock_new_inode(inode);
  1296. return inode;
  1297. bad_inode:
  1298. iget_failed(inode);
  1299. return ERR_PTR(ret);
  1300. }
  1301. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1302. {
  1303. struct ext2_inode_info *ei = EXT2_I(inode);
  1304. struct super_block *sb = inode->i_sb;
  1305. ino_t ino = inode->i_ino;
  1306. uid_t uid = i_uid_read(inode);
  1307. gid_t gid = i_gid_read(inode);
  1308. struct buffer_head * bh;
  1309. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1310. int n;
  1311. int err = 0;
  1312. if (IS_ERR(raw_inode))
  1313. return -EIO;
  1314. /* For fields not not tracking in the in-memory inode,
  1315. * initialise them to zero for new inodes. */
  1316. if (ei->i_state & EXT2_STATE_NEW)
  1317. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1318. ext2_get_inode_flags(ei);
  1319. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1320. if (!(test_opt(sb, NO_UID32))) {
  1321. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1322. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1323. /*
  1324. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1325. * re-used with the upper 16 bits of the uid/gid intact
  1326. */
  1327. if (!ei->i_dtime) {
  1328. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1329. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1330. } else {
  1331. raw_inode->i_uid_high = 0;
  1332. raw_inode->i_gid_high = 0;
  1333. }
  1334. } else {
  1335. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1336. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1337. raw_inode->i_uid_high = 0;
  1338. raw_inode->i_gid_high = 0;
  1339. }
  1340. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1341. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1342. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1343. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1344. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1345. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1346. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1347. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1348. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1349. raw_inode->i_frag = ei->i_frag_no;
  1350. raw_inode->i_fsize = ei->i_frag_size;
  1351. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1352. if (!S_ISREG(inode->i_mode))
  1353. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1354. else {
  1355. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1356. if (inode->i_size > 0x7fffffffULL) {
  1357. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1358. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1359. EXT2_SB(sb)->s_es->s_rev_level ==
  1360. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1361. /* If this is the first large file
  1362. * created, add a flag to the superblock.
  1363. */
  1364. spin_lock(&EXT2_SB(sb)->s_lock);
  1365. ext2_update_dynamic_rev(sb);
  1366. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1367. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1368. spin_unlock(&EXT2_SB(sb)->s_lock);
  1369. ext2_write_super(sb);
  1370. }
  1371. }
  1372. }
  1373. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1374. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1375. if (old_valid_dev(inode->i_rdev)) {
  1376. raw_inode->i_block[0] =
  1377. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1378. raw_inode->i_block[1] = 0;
  1379. } else {
  1380. raw_inode->i_block[0] = 0;
  1381. raw_inode->i_block[1] =
  1382. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1383. raw_inode->i_block[2] = 0;
  1384. }
  1385. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1386. raw_inode->i_block[n] = ei->i_data[n];
  1387. mark_buffer_dirty(bh);
  1388. if (do_sync) {
  1389. sync_dirty_buffer(bh);
  1390. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1391. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1392. sb->s_id, (unsigned long) ino);
  1393. err = -EIO;
  1394. }
  1395. }
  1396. ei->i_state &= ~EXT2_STATE_NEW;
  1397. brelse (bh);
  1398. return err;
  1399. }
  1400. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1401. {
  1402. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1403. }
  1404. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1405. {
  1406. struct inode *inode = d_inode(dentry);
  1407. int error;
  1408. error = inode_change_ok(inode, iattr);
  1409. if (error)
  1410. return error;
  1411. if (is_quota_modification(inode, iattr)) {
  1412. error = dquot_initialize(inode);
  1413. if (error)
  1414. return error;
  1415. }
  1416. if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  1417. (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
  1418. error = dquot_transfer(inode, iattr);
  1419. if (error)
  1420. return error;
  1421. }
  1422. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1423. error = ext2_setsize(inode, iattr->ia_size);
  1424. if (error)
  1425. return error;
  1426. }
  1427. setattr_copy(inode, iattr);
  1428. if (iattr->ia_valid & ATTR_MODE)
  1429. error = posix_acl_chmod(inode, inode->i_mode);
  1430. mark_inode_dirty(inode);
  1431. return error;
  1432. }