inode.c 48 KB

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