inode.c 45 KB

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