inode.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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(inode);
  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;
  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 -EIO;
  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. partial = chain + depth - 1;
  598. break;
  599. }
  600. blk = le32_to_cpu(*(chain[depth-1].p + count));
  601. if (blk == first_block + count)
  602. count++;
  603. else
  604. break;
  605. }
  606. if (err != -EAGAIN)
  607. goto got_it;
  608. }
  609. /* Next simple case - plain lookup or failed read of indirect block */
  610. if (!create || err == -EIO)
  611. goto cleanup;
  612. mutex_lock(&ei->truncate_mutex);
  613. /*
  614. * If the indirect block is missing while we are reading
  615. * the chain(ext2_get_branch() returns -EAGAIN err), or
  616. * if the chain has been changed after we grab the semaphore,
  617. * (either because another process truncated this branch, or
  618. * another get_block allocated this branch) re-grab the chain to see if
  619. * the request block has been allocated or not.
  620. *
  621. * Since we already block the truncate/other get_block
  622. * at this point, we will have the current copy of the chain when we
  623. * splice the branch into the tree.
  624. */
  625. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  626. while (partial > chain) {
  627. brelse(partial->bh);
  628. partial--;
  629. }
  630. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  631. if (!partial) {
  632. count++;
  633. mutex_unlock(&ei->truncate_mutex);
  634. if (err)
  635. goto cleanup;
  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. * We must unmap blocks before zeroing so that writeback cannot
  666. * overwrite zeros with stale data from block device page cache.
  667. */
  668. clean_bdev_aliases(inode->i_sb->s_bdev,
  669. le32_to_cpu(chain[depth-1].key),
  670. count);
  671. /*
  672. * block must be initialised before we put it in the tree
  673. * so that it's not found by another thread before it's
  674. * initialised
  675. */
  676. err = sb_issue_zeroout(inode->i_sb,
  677. le32_to_cpu(chain[depth-1].key), count,
  678. GFP_NOFS);
  679. if (err) {
  680. mutex_unlock(&ei->truncate_mutex);
  681. goto cleanup;
  682. }
  683. }
  684. *new = true;
  685. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  686. mutex_unlock(&ei->truncate_mutex);
  687. got_it:
  688. if (count > blocks_to_boundary)
  689. *boundary = true;
  690. err = count;
  691. /* Clean up and exit */
  692. partial = chain + depth - 1; /* the whole chain */
  693. cleanup:
  694. while (partial > chain) {
  695. brelse(partial->bh);
  696. partial--;
  697. }
  698. if (err > 0)
  699. *bno = le32_to_cpu(chain[depth-1].key);
  700. return err;
  701. }
  702. int ext2_get_block(struct inode *inode, sector_t iblock,
  703. struct buffer_head *bh_result, int create)
  704. {
  705. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  706. bool new = false, boundary = false;
  707. u32 bno;
  708. int ret;
  709. ret = ext2_get_blocks(inode, iblock, max_blocks, &bno, &new, &boundary,
  710. create);
  711. if (ret <= 0)
  712. return ret;
  713. map_bh(bh_result, inode->i_sb, bno);
  714. bh_result->b_size = (ret << inode->i_blkbits);
  715. if (new)
  716. set_buffer_new(bh_result);
  717. if (boundary)
  718. set_buffer_boundary(bh_result);
  719. return 0;
  720. }
  721. #ifdef CONFIG_FS_DAX
  722. static int ext2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  723. unsigned flags, struct iomap *iomap)
  724. {
  725. struct block_device *bdev;
  726. unsigned int blkbits = inode->i_blkbits;
  727. unsigned long first_block = offset >> blkbits;
  728. unsigned long max_blocks = (length + (1 << blkbits) - 1) >> blkbits;
  729. bool new = false, boundary = false;
  730. u32 bno;
  731. int ret;
  732. ret = ext2_get_blocks(inode, first_block, max_blocks,
  733. &bno, &new, &boundary, flags & IOMAP_WRITE);
  734. if (ret < 0)
  735. return ret;
  736. iomap->flags = 0;
  737. bdev = inode->i_sb->s_bdev;
  738. iomap->bdev = bdev;
  739. iomap->offset = (u64)first_block << blkbits;
  740. if (blk_queue_dax(bdev->bd_queue))
  741. iomap->dax_dev = fs_dax_get_by_host(bdev->bd_disk->disk_name);
  742. else
  743. iomap->dax_dev = NULL;
  744. if (ret == 0) {
  745. iomap->type = IOMAP_HOLE;
  746. iomap->blkno = IOMAP_NULL_BLOCK;
  747. iomap->length = 1 << blkbits;
  748. } else {
  749. iomap->type = IOMAP_MAPPED;
  750. iomap->blkno = (sector_t)bno << (blkbits - 9);
  751. iomap->length = (u64)ret << blkbits;
  752. iomap->flags |= IOMAP_F_MERGED;
  753. }
  754. if (new)
  755. iomap->flags |= IOMAP_F_NEW;
  756. return 0;
  757. }
  758. static int
  759. ext2_iomap_end(struct inode *inode, loff_t offset, loff_t length,
  760. ssize_t written, unsigned flags, struct iomap *iomap)
  761. {
  762. fs_put_dax(iomap->dax_dev);
  763. if (iomap->type == IOMAP_MAPPED &&
  764. written < length &&
  765. (flags & IOMAP_WRITE))
  766. ext2_write_failed(inode->i_mapping, offset + length);
  767. return 0;
  768. }
  769. const struct iomap_ops ext2_iomap_ops = {
  770. .iomap_begin = ext2_iomap_begin,
  771. .iomap_end = ext2_iomap_end,
  772. };
  773. #else
  774. /* Define empty ops for !CONFIG_FS_DAX case to avoid ugly ifdefs */
  775. const struct iomap_ops ext2_iomap_ops;
  776. #endif /* CONFIG_FS_DAX */
  777. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  778. u64 start, u64 len)
  779. {
  780. return generic_block_fiemap(inode, fieinfo, start, len,
  781. ext2_get_block);
  782. }
  783. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  784. {
  785. return block_write_full_page(page, ext2_get_block, wbc);
  786. }
  787. static int ext2_readpage(struct file *file, struct page *page)
  788. {
  789. return mpage_readpage(page, ext2_get_block);
  790. }
  791. static int
  792. ext2_readpages(struct file *file, struct address_space *mapping,
  793. struct list_head *pages, unsigned nr_pages)
  794. {
  795. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  796. }
  797. static int
  798. ext2_write_begin(struct file *file, struct address_space *mapping,
  799. loff_t pos, unsigned len, unsigned flags,
  800. struct page **pagep, void **fsdata)
  801. {
  802. int ret;
  803. ret = block_write_begin(mapping, pos, len, flags, pagep,
  804. ext2_get_block);
  805. if (ret < 0)
  806. ext2_write_failed(mapping, pos + len);
  807. return ret;
  808. }
  809. static int ext2_write_end(struct file *file, struct address_space *mapping,
  810. loff_t pos, unsigned len, unsigned copied,
  811. struct page *page, void *fsdata)
  812. {
  813. int ret;
  814. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  815. if (ret < len)
  816. ext2_write_failed(mapping, pos + len);
  817. return ret;
  818. }
  819. static int
  820. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  821. loff_t pos, unsigned len, unsigned flags,
  822. struct page **pagep, void **fsdata)
  823. {
  824. int ret;
  825. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  826. ext2_get_block);
  827. if (ret < 0)
  828. ext2_write_failed(mapping, pos + len);
  829. return ret;
  830. }
  831. static int ext2_nobh_writepage(struct page *page,
  832. struct writeback_control *wbc)
  833. {
  834. return nobh_writepage(page, ext2_get_block, wbc);
  835. }
  836. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  837. {
  838. return generic_block_bmap(mapping,block,ext2_get_block);
  839. }
  840. static ssize_t
  841. ext2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  842. {
  843. struct file *file = iocb->ki_filp;
  844. struct address_space *mapping = file->f_mapping;
  845. struct inode *inode = mapping->host;
  846. size_t count = iov_iter_count(iter);
  847. loff_t offset = iocb->ki_pos;
  848. ssize_t ret;
  849. if (WARN_ON_ONCE(IS_DAX(inode)))
  850. return -EIO;
  851. ret = blockdev_direct_IO(iocb, inode, iter, ext2_get_block);
  852. if (ret < 0 && iov_iter_rw(iter) == WRITE)
  853. ext2_write_failed(mapping, offset + count);
  854. return ret;
  855. }
  856. static int
  857. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  858. {
  859. #ifdef CONFIG_FS_DAX
  860. if (dax_mapping(mapping)) {
  861. return dax_writeback_mapping_range(mapping,
  862. mapping->host->i_sb->s_bdev,
  863. wbc);
  864. }
  865. #endif
  866. return mpage_writepages(mapping, wbc, ext2_get_block);
  867. }
  868. const struct address_space_operations ext2_aops = {
  869. .readpage = ext2_readpage,
  870. .readpages = ext2_readpages,
  871. .writepage = ext2_writepage,
  872. .write_begin = ext2_write_begin,
  873. .write_end = ext2_write_end,
  874. .bmap = ext2_bmap,
  875. .direct_IO = ext2_direct_IO,
  876. .writepages = ext2_writepages,
  877. .migratepage = buffer_migrate_page,
  878. .is_partially_uptodate = block_is_partially_uptodate,
  879. .error_remove_page = generic_error_remove_page,
  880. };
  881. const struct address_space_operations ext2_nobh_aops = {
  882. .readpage = ext2_readpage,
  883. .readpages = ext2_readpages,
  884. .writepage = ext2_nobh_writepage,
  885. .write_begin = ext2_nobh_write_begin,
  886. .write_end = nobh_write_end,
  887. .bmap = ext2_bmap,
  888. .direct_IO = ext2_direct_IO,
  889. .writepages = ext2_writepages,
  890. .migratepage = buffer_migrate_page,
  891. .error_remove_page = generic_error_remove_page,
  892. };
  893. /*
  894. * Probably it should be a library function... search for first non-zero word
  895. * or memcmp with zero_page, whatever is better for particular architecture.
  896. * Linus?
  897. */
  898. static inline int all_zeroes(__le32 *p, __le32 *q)
  899. {
  900. while (p < q)
  901. if (*p++)
  902. return 0;
  903. return 1;
  904. }
  905. /**
  906. * ext2_find_shared - find the indirect blocks for partial truncation.
  907. * @inode: inode in question
  908. * @depth: depth of the affected branch
  909. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  910. * @chain: place to store the pointers to partial indirect blocks
  911. * @top: place to the (detached) top of branch
  912. *
  913. * This is a helper function used by ext2_truncate().
  914. *
  915. * When we do truncate() we may have to clean the ends of several indirect
  916. * blocks but leave the blocks themselves alive. Block is partially
  917. * truncated if some data below the new i_size is referred from it (and
  918. * it is on the path to the first completely truncated data block, indeed).
  919. * We have to free the top of that path along with everything to the right
  920. * of the path. Since no allocation past the truncation point is possible
  921. * until ext2_truncate() finishes, we may safely do the latter, but top
  922. * of branch may require special attention - pageout below the truncation
  923. * point might try to populate it.
  924. *
  925. * We atomically detach the top of branch from the tree, store the block
  926. * number of its root in *@top, pointers to buffer_heads of partially
  927. * truncated blocks - in @chain[].bh and pointers to their last elements
  928. * that should not be removed - in @chain[].p. Return value is the pointer
  929. * to last filled element of @chain.
  930. *
  931. * The work left to caller to do the actual freeing of subtrees:
  932. * a) free the subtree starting from *@top
  933. * b) free the subtrees whose roots are stored in
  934. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  935. * c) free the subtrees growing from the inode past the @chain[0].p
  936. * (no partially truncated stuff there).
  937. */
  938. static Indirect *ext2_find_shared(struct inode *inode,
  939. int depth,
  940. int offsets[4],
  941. Indirect chain[4],
  942. __le32 *top)
  943. {
  944. Indirect *partial, *p;
  945. int k, err;
  946. *top = 0;
  947. for (k = depth; k > 1 && !offsets[k-1]; k--)
  948. ;
  949. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  950. if (!partial)
  951. partial = chain + k-1;
  952. /*
  953. * If the branch acquired continuation since we've looked at it -
  954. * fine, it should all survive and (new) top doesn't belong to us.
  955. */
  956. write_lock(&EXT2_I(inode)->i_meta_lock);
  957. if (!partial->key && *partial->p) {
  958. write_unlock(&EXT2_I(inode)->i_meta_lock);
  959. goto no_top;
  960. }
  961. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  962. ;
  963. /*
  964. * OK, we've found the last block that must survive. The rest of our
  965. * branch should be detached before unlocking. However, if that rest
  966. * of branch is all ours and does not grow immediately from the inode
  967. * it's easier to cheat and just decrement partial->p.
  968. */
  969. if (p == chain + k - 1 && p > chain) {
  970. p->p--;
  971. } else {
  972. *top = *p->p;
  973. *p->p = 0;
  974. }
  975. write_unlock(&EXT2_I(inode)->i_meta_lock);
  976. while(partial > p)
  977. {
  978. brelse(partial->bh);
  979. partial--;
  980. }
  981. no_top:
  982. return partial;
  983. }
  984. /**
  985. * ext2_free_data - free a list of data blocks
  986. * @inode: inode we are dealing with
  987. * @p: array of block numbers
  988. * @q: points immediately past the end of array
  989. *
  990. * We are freeing all blocks referred from that array (numbers are
  991. * stored as little-endian 32-bit) and updating @inode->i_blocks
  992. * appropriately.
  993. */
  994. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  995. {
  996. unsigned long block_to_free = 0, count = 0;
  997. unsigned long nr;
  998. for ( ; p < q ; p++) {
  999. nr = le32_to_cpu(*p);
  1000. if (nr) {
  1001. *p = 0;
  1002. /* accumulate blocks to free if they're contiguous */
  1003. if (count == 0)
  1004. goto free_this;
  1005. else if (block_to_free == nr - count)
  1006. count++;
  1007. else {
  1008. ext2_free_blocks (inode, block_to_free, count);
  1009. mark_inode_dirty(inode);
  1010. free_this:
  1011. block_to_free = nr;
  1012. count = 1;
  1013. }
  1014. }
  1015. }
  1016. if (count > 0) {
  1017. ext2_free_blocks (inode, block_to_free, count);
  1018. mark_inode_dirty(inode);
  1019. }
  1020. }
  1021. /**
  1022. * ext2_free_branches - free an array of branches
  1023. * @inode: inode we are dealing with
  1024. * @p: array of block numbers
  1025. * @q: pointer immediately past the end of array
  1026. * @depth: depth of the branches to free
  1027. *
  1028. * We are freeing all blocks referred from these branches (numbers are
  1029. * stored as little-endian 32-bit) and updating @inode->i_blocks
  1030. * appropriately.
  1031. */
  1032. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  1033. {
  1034. struct buffer_head * bh;
  1035. unsigned long nr;
  1036. if (depth--) {
  1037. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1038. for ( ; p < q ; p++) {
  1039. nr = le32_to_cpu(*p);
  1040. if (!nr)
  1041. continue;
  1042. *p = 0;
  1043. bh = sb_bread(inode->i_sb, nr);
  1044. /*
  1045. * A read failure? Report error and clear slot
  1046. * (should be rare).
  1047. */
  1048. if (!bh) {
  1049. ext2_error(inode->i_sb, "ext2_free_branches",
  1050. "Read failure, inode=%ld, block=%ld",
  1051. inode->i_ino, nr);
  1052. continue;
  1053. }
  1054. ext2_free_branches(inode,
  1055. (__le32*)bh->b_data,
  1056. (__le32*)bh->b_data + addr_per_block,
  1057. depth);
  1058. bforget(bh);
  1059. ext2_free_blocks(inode, nr, 1);
  1060. mark_inode_dirty(inode);
  1061. }
  1062. } else
  1063. ext2_free_data(inode, p, q);
  1064. }
  1065. /* dax_sem must be held when calling this function */
  1066. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1067. {
  1068. __le32 *i_data = EXT2_I(inode)->i_data;
  1069. struct ext2_inode_info *ei = EXT2_I(inode);
  1070. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1071. int offsets[4];
  1072. Indirect chain[4];
  1073. Indirect *partial;
  1074. __le32 nr = 0;
  1075. int n;
  1076. long iblock;
  1077. unsigned blocksize;
  1078. blocksize = inode->i_sb->s_blocksize;
  1079. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  1080. #ifdef CONFIG_FS_DAX
  1081. WARN_ON(!rwsem_is_locked(&ei->dax_sem));
  1082. #endif
  1083. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  1084. if (n == 0)
  1085. return;
  1086. /*
  1087. * From here we block out all ext2_get_block() callers who want to
  1088. * modify the block allocation tree.
  1089. */
  1090. mutex_lock(&ei->truncate_mutex);
  1091. if (n == 1) {
  1092. ext2_free_data(inode, i_data+offsets[0],
  1093. i_data + EXT2_NDIR_BLOCKS);
  1094. goto do_indirects;
  1095. }
  1096. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  1097. /* Kill the top of shared branch (already detached) */
  1098. if (nr) {
  1099. if (partial == chain)
  1100. mark_inode_dirty(inode);
  1101. else
  1102. mark_buffer_dirty_inode(partial->bh, inode);
  1103. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1104. }
  1105. /* Clear the ends of indirect blocks on the shared branch */
  1106. while (partial > chain) {
  1107. ext2_free_branches(inode,
  1108. partial->p + 1,
  1109. (__le32*)partial->bh->b_data+addr_per_block,
  1110. (chain+n-1) - partial);
  1111. mark_buffer_dirty_inode(partial->bh, inode);
  1112. brelse (partial->bh);
  1113. partial--;
  1114. }
  1115. do_indirects:
  1116. /* Kill the remaining (whole) subtrees */
  1117. switch (offsets[0]) {
  1118. default:
  1119. nr = i_data[EXT2_IND_BLOCK];
  1120. if (nr) {
  1121. i_data[EXT2_IND_BLOCK] = 0;
  1122. mark_inode_dirty(inode);
  1123. ext2_free_branches(inode, &nr, &nr+1, 1);
  1124. }
  1125. case EXT2_IND_BLOCK:
  1126. nr = i_data[EXT2_DIND_BLOCK];
  1127. if (nr) {
  1128. i_data[EXT2_DIND_BLOCK] = 0;
  1129. mark_inode_dirty(inode);
  1130. ext2_free_branches(inode, &nr, &nr+1, 2);
  1131. }
  1132. case EXT2_DIND_BLOCK:
  1133. nr = i_data[EXT2_TIND_BLOCK];
  1134. if (nr) {
  1135. i_data[EXT2_TIND_BLOCK] = 0;
  1136. mark_inode_dirty(inode);
  1137. ext2_free_branches(inode, &nr, &nr+1, 3);
  1138. }
  1139. case EXT2_TIND_BLOCK:
  1140. ;
  1141. }
  1142. ext2_discard_reservation(inode);
  1143. mutex_unlock(&ei->truncate_mutex);
  1144. }
  1145. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1146. {
  1147. /*
  1148. * XXX: it seems like a bug here that we don't allow
  1149. * IS_APPEND inode to have blocks-past-i_size trimmed off.
  1150. * review and fix this.
  1151. *
  1152. * Also would be nice to be able to handle IO errors and such,
  1153. * but that's probably too much to ask.
  1154. */
  1155. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1156. S_ISLNK(inode->i_mode)))
  1157. return;
  1158. if (ext2_inode_is_fast_symlink(inode))
  1159. return;
  1160. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1161. return;
  1162. dax_sem_down_write(EXT2_I(inode));
  1163. __ext2_truncate_blocks(inode, offset);
  1164. dax_sem_up_write(EXT2_I(inode));
  1165. }
  1166. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1167. {
  1168. int error;
  1169. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1170. S_ISLNK(inode->i_mode)))
  1171. return -EINVAL;
  1172. if (ext2_inode_is_fast_symlink(inode))
  1173. return -EINVAL;
  1174. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1175. return -EPERM;
  1176. inode_dio_wait(inode);
  1177. if (IS_DAX(inode)) {
  1178. error = iomap_zero_range(inode, newsize,
  1179. PAGE_ALIGN(newsize) - newsize, NULL,
  1180. &ext2_iomap_ops);
  1181. } else if (test_opt(inode->i_sb, NOBH))
  1182. error = nobh_truncate_page(inode->i_mapping,
  1183. newsize, ext2_get_block);
  1184. else
  1185. error = block_truncate_page(inode->i_mapping,
  1186. newsize, ext2_get_block);
  1187. if (error)
  1188. return error;
  1189. dax_sem_down_write(EXT2_I(inode));
  1190. truncate_setsize(inode, newsize);
  1191. __ext2_truncate_blocks(inode, newsize);
  1192. dax_sem_up_write(EXT2_I(inode));
  1193. inode->i_mtime = inode->i_ctime = current_time(inode);
  1194. if (inode_needs_sync(inode)) {
  1195. sync_mapping_buffers(inode->i_mapping);
  1196. sync_inode_metadata(inode, 1);
  1197. } else {
  1198. mark_inode_dirty(inode);
  1199. }
  1200. return 0;
  1201. }
  1202. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1203. struct buffer_head **p)
  1204. {
  1205. struct buffer_head * bh;
  1206. unsigned long block_group;
  1207. unsigned long block;
  1208. unsigned long offset;
  1209. struct ext2_group_desc * gdp;
  1210. *p = NULL;
  1211. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1212. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1213. goto Einval;
  1214. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1215. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1216. if (!gdp)
  1217. goto Egdp;
  1218. /*
  1219. * Figure out the offset within the block group inode table
  1220. */
  1221. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1222. block = le32_to_cpu(gdp->bg_inode_table) +
  1223. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1224. if (!(bh = sb_bread(sb, block)))
  1225. goto Eio;
  1226. *p = bh;
  1227. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1228. return (struct ext2_inode *) (bh->b_data + offset);
  1229. Einval:
  1230. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1231. (unsigned long) ino);
  1232. return ERR_PTR(-EINVAL);
  1233. Eio:
  1234. ext2_error(sb, "ext2_get_inode",
  1235. "unable to read inode block - inode=%lu, block=%lu",
  1236. (unsigned long) ino, block);
  1237. Egdp:
  1238. return ERR_PTR(-EIO);
  1239. }
  1240. void ext2_set_inode_flags(struct inode *inode)
  1241. {
  1242. unsigned int flags = EXT2_I(inode)->i_flags;
  1243. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  1244. S_DIRSYNC | S_DAX);
  1245. if (flags & EXT2_SYNC_FL)
  1246. inode->i_flags |= S_SYNC;
  1247. if (flags & EXT2_APPEND_FL)
  1248. inode->i_flags |= S_APPEND;
  1249. if (flags & EXT2_IMMUTABLE_FL)
  1250. inode->i_flags |= S_IMMUTABLE;
  1251. if (flags & EXT2_NOATIME_FL)
  1252. inode->i_flags |= S_NOATIME;
  1253. if (flags & EXT2_DIRSYNC_FL)
  1254. inode->i_flags |= S_DIRSYNC;
  1255. if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
  1256. inode->i_flags |= S_DAX;
  1257. }
  1258. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1259. {
  1260. struct ext2_inode_info *ei;
  1261. struct buffer_head * bh;
  1262. struct ext2_inode *raw_inode;
  1263. struct inode *inode;
  1264. long ret = -EIO;
  1265. int n;
  1266. uid_t i_uid;
  1267. gid_t i_gid;
  1268. inode = iget_locked(sb, ino);
  1269. if (!inode)
  1270. return ERR_PTR(-ENOMEM);
  1271. if (!(inode->i_state & I_NEW))
  1272. return inode;
  1273. ei = EXT2_I(inode);
  1274. ei->i_block_alloc_info = NULL;
  1275. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1276. if (IS_ERR(raw_inode)) {
  1277. ret = PTR_ERR(raw_inode);
  1278. goto bad_inode;
  1279. }
  1280. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1281. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1282. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1283. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1284. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1285. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1286. }
  1287. i_uid_write(inode, i_uid);
  1288. i_gid_write(inode, i_gid);
  1289. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1290. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1291. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1292. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1293. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1294. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1295. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1296. /* We now have enough fields to check if the inode was active or not.
  1297. * This is needed because nfsd might try to access dead inodes
  1298. * the test is that same one that e2fsck uses
  1299. * NeilBrown 1999oct15
  1300. */
  1301. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1302. /* this inode is deleted */
  1303. brelse (bh);
  1304. ret = -ESTALE;
  1305. goto bad_inode;
  1306. }
  1307. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1308. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1309. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1310. ei->i_frag_no = raw_inode->i_frag;
  1311. ei->i_frag_size = raw_inode->i_fsize;
  1312. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1313. ei->i_dir_acl = 0;
  1314. if (ei->i_file_acl &&
  1315. !ext2_data_block_valid(EXT2_SB(sb), ei->i_file_acl, 1)) {
  1316. ext2_error(sb, "ext2_iget", "bad extended attribute block %u",
  1317. ei->i_file_acl);
  1318. brelse(bh);
  1319. ret = -EFSCORRUPTED;
  1320. goto bad_inode;
  1321. }
  1322. if (S_ISREG(inode->i_mode))
  1323. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1324. else
  1325. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1326. if (i_size_read(inode) < 0) {
  1327. ret = -EFSCORRUPTED;
  1328. goto bad_inode;
  1329. }
  1330. ei->i_dtime = 0;
  1331. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1332. ei->i_state = 0;
  1333. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1334. ei->i_dir_start_lookup = 0;
  1335. /*
  1336. * NOTE! The in-memory inode i_data array is in little-endian order
  1337. * even on big-endian machines: we do NOT byteswap the block numbers!
  1338. */
  1339. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1340. ei->i_data[n] = raw_inode->i_block[n];
  1341. if (S_ISREG(inode->i_mode)) {
  1342. inode->i_op = &ext2_file_inode_operations;
  1343. if (test_opt(inode->i_sb, NOBH)) {
  1344. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1345. inode->i_fop = &ext2_file_operations;
  1346. } else {
  1347. inode->i_mapping->a_ops = &ext2_aops;
  1348. inode->i_fop = &ext2_file_operations;
  1349. }
  1350. } else if (S_ISDIR(inode->i_mode)) {
  1351. inode->i_op = &ext2_dir_inode_operations;
  1352. inode->i_fop = &ext2_dir_operations;
  1353. if (test_opt(inode->i_sb, NOBH))
  1354. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1355. else
  1356. inode->i_mapping->a_ops = &ext2_aops;
  1357. } else if (S_ISLNK(inode->i_mode)) {
  1358. if (ext2_inode_is_fast_symlink(inode)) {
  1359. inode->i_link = (char *)ei->i_data;
  1360. inode->i_op = &ext2_fast_symlink_inode_operations;
  1361. nd_terminate_link(ei->i_data, inode->i_size,
  1362. sizeof(ei->i_data) - 1);
  1363. } else {
  1364. inode->i_op = &ext2_symlink_inode_operations;
  1365. inode_nohighmem(inode);
  1366. if (test_opt(inode->i_sb, NOBH))
  1367. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1368. else
  1369. inode->i_mapping->a_ops = &ext2_aops;
  1370. }
  1371. } else {
  1372. inode->i_op = &ext2_special_inode_operations;
  1373. if (raw_inode->i_block[0])
  1374. init_special_inode(inode, inode->i_mode,
  1375. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1376. else
  1377. init_special_inode(inode, inode->i_mode,
  1378. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1379. }
  1380. brelse (bh);
  1381. ext2_set_inode_flags(inode);
  1382. unlock_new_inode(inode);
  1383. return inode;
  1384. bad_inode:
  1385. iget_failed(inode);
  1386. return ERR_PTR(ret);
  1387. }
  1388. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1389. {
  1390. struct ext2_inode_info *ei = EXT2_I(inode);
  1391. struct super_block *sb = inode->i_sb;
  1392. ino_t ino = inode->i_ino;
  1393. uid_t uid = i_uid_read(inode);
  1394. gid_t gid = i_gid_read(inode);
  1395. struct buffer_head * bh;
  1396. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1397. int n;
  1398. int err = 0;
  1399. if (IS_ERR(raw_inode))
  1400. return -EIO;
  1401. /* For fields not not tracking in the in-memory inode,
  1402. * initialise them to zero for new inodes. */
  1403. if (ei->i_state & EXT2_STATE_NEW)
  1404. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1405. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1406. if (!(test_opt(sb, NO_UID32))) {
  1407. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1408. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1409. /*
  1410. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1411. * re-used with the upper 16 bits of the uid/gid intact
  1412. */
  1413. if (!ei->i_dtime) {
  1414. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1415. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1416. } else {
  1417. raw_inode->i_uid_high = 0;
  1418. raw_inode->i_gid_high = 0;
  1419. }
  1420. } else {
  1421. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1422. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1423. raw_inode->i_uid_high = 0;
  1424. raw_inode->i_gid_high = 0;
  1425. }
  1426. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1427. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1428. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1429. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1430. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1431. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1432. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1433. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1434. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1435. raw_inode->i_frag = ei->i_frag_no;
  1436. raw_inode->i_fsize = ei->i_frag_size;
  1437. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1438. if (!S_ISREG(inode->i_mode))
  1439. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1440. else {
  1441. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1442. if (inode->i_size > 0x7fffffffULL) {
  1443. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1444. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1445. EXT2_SB(sb)->s_es->s_rev_level ==
  1446. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1447. /* If this is the first large file
  1448. * created, add a flag to the superblock.
  1449. */
  1450. spin_lock(&EXT2_SB(sb)->s_lock);
  1451. ext2_update_dynamic_rev(sb);
  1452. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1453. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1454. spin_unlock(&EXT2_SB(sb)->s_lock);
  1455. ext2_sync_super(sb, EXT2_SB(sb)->s_es, 1);
  1456. }
  1457. }
  1458. }
  1459. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1460. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1461. if (old_valid_dev(inode->i_rdev)) {
  1462. raw_inode->i_block[0] =
  1463. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1464. raw_inode->i_block[1] = 0;
  1465. } else {
  1466. raw_inode->i_block[0] = 0;
  1467. raw_inode->i_block[1] =
  1468. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1469. raw_inode->i_block[2] = 0;
  1470. }
  1471. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1472. raw_inode->i_block[n] = ei->i_data[n];
  1473. mark_buffer_dirty(bh);
  1474. if (do_sync) {
  1475. sync_dirty_buffer(bh);
  1476. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1477. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1478. sb->s_id, (unsigned long) ino);
  1479. err = -EIO;
  1480. }
  1481. }
  1482. ei->i_state &= ~EXT2_STATE_NEW;
  1483. brelse (bh);
  1484. return err;
  1485. }
  1486. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1487. {
  1488. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1489. }
  1490. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1491. {
  1492. struct inode *inode = d_inode(dentry);
  1493. int error;
  1494. error = setattr_prepare(dentry, iattr);
  1495. if (error)
  1496. return error;
  1497. if (is_quota_modification(inode, iattr)) {
  1498. error = dquot_initialize(inode);
  1499. if (error)
  1500. return error;
  1501. }
  1502. if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  1503. (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
  1504. error = dquot_transfer(inode, iattr);
  1505. if (error)
  1506. return error;
  1507. }
  1508. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1509. error = ext2_setsize(inode, iattr->ia_size);
  1510. if (error)
  1511. return error;
  1512. }
  1513. setattr_copy(inode, iattr);
  1514. if (iattr->ia_valid & ATTR_MODE)
  1515. error = posix_acl_chmod(inode, inode->i_mode);
  1516. mark_inode_dirty(inode);
  1517. return error;
  1518. }