inode.c 47 KB

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