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