inode.c 46 KB

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