Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
a844f451 | 26 | #include "xfs_ag.h" |
1da177e4 LT |
27 | #include "xfs_dir2.h" |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_mount.h" | |
1da177e4 | 30 | #include "xfs_bmap_btree.h" |
a844f451 | 31 | #include "xfs_alloc_btree.h" |
1da177e4 | 32 | #include "xfs_ialloc_btree.h" |
1da177e4 | 33 | #include "xfs_dir2_sf.h" |
a844f451 | 34 | #include "xfs_attr_sf.h" |
1da177e4 LT |
35 | #include "xfs_dinode.h" |
36 | #include "xfs_inode.h" | |
37 | #include "xfs_ialloc.h" | |
38 | #include "xfs_itable.h" | |
39 | #include "xfs_error.h" | |
a844f451 | 40 | #include "xfs_btree.h" |
1da177e4 | 41 | |
d96f8f89 | 42 | STATIC int |
6f1f2168 VA |
43 | xfs_internal_inum( |
44 | xfs_mount_t *mp, | |
45 | xfs_ino_t ino) | |
46 | { | |
47 | return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino || | |
62118709 | 48 | (xfs_sb_version_hasquota(&mp->m_sb) && |
6f1f2168 VA |
49 | (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino))); |
50 | } | |
51 | ||
7dce11db CH |
52 | /* |
53 | * Return stat information for one inode. | |
54 | * Return 0 if ok, else errno. | |
55 | */ | |
56 | int | |
57 | xfs_bulkstat_one_int( | |
58 | struct xfs_mount *mp, /* mount point for filesystem */ | |
59 | xfs_ino_t ino, /* inode to get data for */ | |
60 | void __user *buffer, /* buffer to place output in */ | |
61 | int ubsize, /* size of buffer */ | |
62 | bulkstat_one_fmt_pf formatter, /* formatter, copy to user */ | |
63 | xfs_daddr_t bno, /* starting bno of cluster */ | |
64 | int *ubused, /* bytes used by me */ | |
65 | int *stat) /* BULKSTAT_RV_... */ | |
1da177e4 | 66 | { |
7dce11db CH |
67 | struct xfs_icdinode *dic; /* dinode core info pointer */ |
68 | struct xfs_inode *ip; /* incore inode pointer */ | |
69 | struct inode *inode; | |
70 | struct xfs_bstat *buf; /* return buffer */ | |
71 | int error = 0; /* error value */ | |
72 | ||
73 | *stat = BULKSTAT_RV_NOTHING; | |
74 | ||
75 | if (!buffer || xfs_internal_inum(mp, ino)) | |
76 | return XFS_ERROR(EINVAL); | |
77 | ||
78 | buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL); | |
79 | if (!buf) | |
80 | return XFS_ERROR(ENOMEM); | |
1da177e4 | 81 | |
745b1f47 NS |
82 | error = xfs_iget(mp, NULL, ino, |
83 | XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno); | |
1da177e4 LT |
84 | if (error) { |
85 | *stat = BULKSTAT_RV_NOTHING; | |
7dce11db | 86 | goto out_free; |
1da177e4 LT |
87 | } |
88 | ||
89 | ASSERT(ip != NULL); | |
92bfc6e7 | 90 | ASSERT(ip->i_imap.im_blkno != 0); |
1da177e4 LT |
91 | |
92 | dic = &ip->i_d; | |
f9581b14 | 93 | inode = VFS_I(ip); |
1da177e4 LT |
94 | |
95 | /* xfs_iget returns the following without needing | |
96 | * further change. | |
97 | */ | |
98 | buf->bs_nlink = dic->di_nlink; | |
99 | buf->bs_projid = dic->di_projid; | |
100 | buf->bs_ino = ino; | |
101 | buf->bs_mode = dic->di_mode; | |
102 | buf->bs_uid = dic->di_uid; | |
103 | buf->bs_gid = dic->di_gid; | |
104 | buf->bs_size = dic->di_size; | |
f9581b14 | 105 | |
8fab451e | 106 | /* |
f9581b14 CH |
107 | * We need to read the timestamps from the Linux inode because |
108 | * the VFS keeps writing directly into the inode structure instead | |
109 | * of telling us about the updates. | |
8fab451e | 110 | */ |
f9581b14 CH |
111 | buf->bs_atime.tv_sec = inode->i_atime.tv_sec; |
112 | buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec; | |
113 | buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec; | |
114 | buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec; | |
115 | buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec; | |
116 | buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec; | |
117 | ||
1da177e4 LT |
118 | buf->bs_xflags = xfs_ip2xflags(ip); |
119 | buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog; | |
120 | buf->bs_extents = dic->di_nextents; | |
121 | buf->bs_gen = dic->di_gen; | |
122 | memset(buf->bs_pad, 0, sizeof(buf->bs_pad)); | |
123 | buf->bs_dmevmask = dic->di_dmevmask; | |
124 | buf->bs_dmstate = dic->di_dmstate; | |
125 | buf->bs_aextents = dic->di_anextents; | |
07000ee6 | 126 | buf->bs_forkoff = XFS_IFORK_BOFF(ip); |
1da177e4 LT |
127 | |
128 | switch (dic->di_format) { | |
129 | case XFS_DINODE_FMT_DEV: | |
130 | buf->bs_rdev = ip->i_df.if_u2.if_rdev; | |
131 | buf->bs_blksize = BLKDEV_IOSIZE; | |
132 | buf->bs_blocks = 0; | |
133 | break; | |
134 | case XFS_DINODE_FMT_LOCAL: | |
135 | case XFS_DINODE_FMT_UUID: | |
136 | buf->bs_rdev = 0; | |
137 | buf->bs_blksize = mp->m_sb.sb_blocksize; | |
138 | buf->bs_blocks = 0; | |
139 | break; | |
140 | case XFS_DINODE_FMT_EXTENTS: | |
141 | case XFS_DINODE_FMT_BTREE: | |
142 | buf->bs_rdev = 0; | |
143 | buf->bs_blksize = mp->m_sb.sb_blocksize; | |
144 | buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks; | |
145 | break; | |
146 | } | |
1da177e4 | 147 | xfs_iput(ip, XFS_ILOCK_SHARED); |
1da177e4 | 148 | |
7dce11db | 149 | error = formatter(buffer, ubsize, ubused, buf); |
1da177e4 | 150 | |
7dce11db CH |
151 | if (!error) |
152 | *stat = BULKSTAT_RV_DIDONE; | |
1da177e4 | 153 | |
7dce11db CH |
154 | out_free: |
155 | kmem_free(buf); | |
156 | return error; | |
1da177e4 LT |
157 | } |
158 | ||
65fbaf24 | 159 | /* Return 0 on success or positive error */ |
faa63e95 MM |
160 | STATIC int |
161 | xfs_bulkstat_one_fmt( | |
162 | void __user *ubuffer, | |
65fbaf24 | 163 | int ubsize, |
164 | int *ubused, | |
faa63e95 MM |
165 | const xfs_bstat_t *buffer) |
166 | { | |
65fbaf24 | 167 | if (ubsize < sizeof(*buffer)) |
168 | return XFS_ERROR(ENOMEM); | |
faa63e95 | 169 | if (copy_to_user(ubuffer, buffer, sizeof(*buffer))) |
65fbaf24 | 170 | return XFS_ERROR(EFAULT); |
171 | if (ubused) | |
172 | *ubused = sizeof(*buffer); | |
173 | return 0; | |
faa63e95 MM |
174 | } |
175 | ||
2ee4fa5c | 176 | int |
177 | xfs_bulkstat_one( | |
178 | xfs_mount_t *mp, /* mount point for filesystem */ | |
179 | xfs_ino_t ino, /* inode number to get data for */ | |
180 | void __user *buffer, /* buffer to place output in */ | |
181 | int ubsize, /* size of buffer */ | |
2ee4fa5c | 182 | xfs_daddr_t bno, /* starting bno of inode cluster */ |
183 | int *ubused, /* bytes used by me */ | |
2ee4fa5c | 184 | int *stat) /* BULKSTAT_RV_... */ |
185 | { | |
186 | return xfs_bulkstat_one_int(mp, ino, buffer, ubsize, | |
187 | xfs_bulkstat_one_fmt, bno, | |
7dce11db | 188 | ubused, stat); |
8b56f083 NS |
189 | } |
190 | ||
cd57e594 LM |
191 | #define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size) |
192 | ||
1da177e4 LT |
193 | /* |
194 | * Return stat information in bulk (by-inode) for the filesystem. | |
195 | */ | |
196 | int /* error status */ | |
197 | xfs_bulkstat( | |
198 | xfs_mount_t *mp, /* mount point for filesystem */ | |
199 | xfs_ino_t *lastinop, /* last inode returned */ | |
200 | int *ubcountp, /* size of buffer/count returned */ | |
201 | bulkstat_one_pf formatter, /* func that'd fill a single buf */ | |
1da177e4 LT |
202 | size_t statstruct_size, /* sizeof struct filling */ |
203 | char __user *ubuffer, /* buffer with inode stats */ | |
c41564b5 | 204 | int *done) /* 1 if there are more stats to get */ |
1da177e4 LT |
205 | { |
206 | xfs_agblock_t agbno=0;/* allocation group block number */ | |
207 | xfs_buf_t *agbp; /* agi header buffer */ | |
208 | xfs_agi_t *agi; /* agi header data */ | |
209 | xfs_agino_t agino; /* inode # in allocation group */ | |
210 | xfs_agnumber_t agno; /* allocation group number */ | |
211 | xfs_daddr_t bno; /* inode cluster start daddr */ | |
212 | int chunkidx; /* current index into inode chunk */ | |
213 | int clustidx; /* current index into inode cluster */ | |
214 | xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */ | |
215 | int end_of_ag; /* set if we've seen the ag end */ | |
216 | int error; /* error code */ | |
217 | int fmterror;/* bulkstat formatter result */ | |
1da177e4 LT |
218 | int i; /* loop index */ |
219 | int icount; /* count of inodes good in irbuf */ | |
215101c3 | 220 | size_t irbsize; /* size of irec buffer in bytes */ |
1da177e4 | 221 | xfs_ino_t ino; /* inode number (filesystem) */ |
26275093 NS |
222 | xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */ |
223 | xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */ | |
224 | xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */ | |
cd57e594 | 225 | xfs_ino_t lastino; /* last inode number returned */ |
1da177e4 LT |
226 | int nbcluster; /* # of blocks in a cluster */ |
227 | int nicluster; /* # of inodes in a cluster */ | |
228 | int nimask; /* mask for inode clusters */ | |
229 | int nirbuf; /* size of irbuf */ | |
230 | int rval; /* return value error code */ | |
231 | int tmp; /* result value from btree calls */ | |
232 | int ubcount; /* size of user's buffer */ | |
233 | int ubleft; /* bytes left in user's buffer */ | |
234 | char __user *ubufp; /* pointer into user's buffer */ | |
235 | int ubelem; /* spaces used in user's buffer */ | |
236 | int ubused; /* bytes used by formatter */ | |
237 | xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */ | |
1da177e4 LT |
238 | |
239 | /* | |
240 | * Get the last inode value, see if there's nothing to do. | |
241 | */ | |
242 | ino = (xfs_ino_t)*lastinop; | |
cd57e594 | 243 | lastino = ino; |
1da177e4 LT |
244 | agno = XFS_INO_TO_AGNO(mp, ino); |
245 | agino = XFS_INO_TO_AGINO(mp, ino); | |
246 | if (agno >= mp->m_sb.sb_agcount || | |
247 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | |
248 | *done = 1; | |
249 | *ubcountp = 0; | |
250 | return 0; | |
251 | } | |
cd57e594 LM |
252 | if (!ubcountp || *ubcountp <= 0) { |
253 | return EINVAL; | |
254 | } | |
1da177e4 LT |
255 | ubcount = *ubcountp; /* statstruct's */ |
256 | ubleft = ubcount * statstruct_size; /* bytes */ | |
257 | *ubcountp = ubelem = 0; | |
258 | *done = 0; | |
259 | fmterror = 0; | |
260 | ubufp = ubuffer; | |
261 | nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ? | |
262 | mp->m_sb.sb_inopblock : | |
263 | (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog); | |
264 | nimask = ~(nicluster - 1); | |
265 | nbcluster = nicluster >> mp->m_sb.sb_inopblog; | |
bdfb0430 CH |
266 | irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4); |
267 | if (!irbuf) | |
268 | return ENOMEM; | |
269 | ||
bb3c7d29 NS |
270 | nirbuf = irbsize / sizeof(*irbuf); |
271 | ||
1da177e4 LT |
272 | /* |
273 | * Loop over the allocation groups, starting from the last | |
274 | * inode returned; 0 means start of the allocation group. | |
275 | */ | |
276 | rval = 0; | |
cd57e594 LM |
277 | while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) { |
278 | cond_resched(); | |
1da177e4 | 279 | bp = NULL; |
1da177e4 | 280 | error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); |
1da177e4 LT |
281 | if (error) { |
282 | /* | |
283 | * Skip this allocation group and go to the next one. | |
284 | */ | |
285 | agno++; | |
286 | agino = 0; | |
287 | continue; | |
288 | } | |
289 | agi = XFS_BUF_TO_AGI(agbp); | |
290 | /* | |
291 | * Allocate and initialize a btree cursor for ialloc btree. | |
292 | */ | |
561f7d17 | 293 | cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno); |
1da177e4 LT |
294 | irbp = irbuf; |
295 | irbufend = irbuf + nirbuf; | |
296 | end_of_ag = 0; | |
297 | /* | |
298 | * If we're returning in the middle of an allocation group, | |
299 | * we need to get the remainder of the chunk we're in. | |
300 | */ | |
301 | if (agino > 0) { | |
2e287a73 CH |
302 | xfs_inobt_rec_incore_t r; |
303 | ||
1da177e4 LT |
304 | /* |
305 | * Lookup the inode chunk that this inode lives in. | |
306 | */ | |
21875505 CH |
307 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, |
308 | &tmp); | |
1da177e4 LT |
309 | if (!error && /* no I/O error */ |
310 | tmp && /* lookup succeeded */ | |
311 | /* got the record, should always work */ | |
2e287a73 | 312 | !(error = xfs_inobt_get_rec(cur, &r, &i)) && |
1da177e4 LT |
313 | i == 1 && |
314 | /* this is the right chunk */ | |
2e287a73 | 315 | agino < r.ir_startino + XFS_INODES_PER_CHUNK && |
1da177e4 | 316 | /* lastino was not last in chunk */ |
2e287a73 | 317 | (chunkidx = agino - r.ir_startino + 1) < |
1da177e4 LT |
318 | XFS_INODES_PER_CHUNK && |
319 | /* there are some left allocated */ | |
9d87c319 | 320 | xfs_inobt_maskn(chunkidx, |
2e287a73 CH |
321 | XFS_INODES_PER_CHUNK - chunkidx) & |
322 | ~r.ir_free) { | |
1da177e4 LT |
323 | /* |
324 | * Grab the chunk record. Mark all the | |
325 | * uninteresting inodes (because they're | |
326 | * before our start point) free. | |
327 | */ | |
328 | for (i = 0; i < chunkidx; i++) { | |
2e287a73 CH |
329 | if (XFS_INOBT_MASK(i) & ~r.ir_free) |
330 | r.ir_freecount++; | |
1da177e4 | 331 | } |
2e287a73 CH |
332 | r.ir_free |= xfs_inobt_maskn(0, chunkidx); |
333 | irbp->ir_startino = r.ir_startino; | |
334 | irbp->ir_freecount = r.ir_freecount; | |
335 | irbp->ir_free = r.ir_free; | |
1da177e4 | 336 | irbp++; |
2e287a73 CH |
337 | agino = r.ir_startino + XFS_INODES_PER_CHUNK; |
338 | icount = XFS_INODES_PER_CHUNK - r.ir_freecount; | |
1da177e4 LT |
339 | } else { |
340 | /* | |
341 | * If any of those tests failed, bump the | |
342 | * inode number (just in case). | |
343 | */ | |
344 | agino++; | |
345 | icount = 0; | |
346 | } | |
347 | /* | |
348 | * In any case, increment to the next record. | |
349 | */ | |
350 | if (!error) | |
637aa50f | 351 | error = xfs_btree_increment(cur, 0, &tmp); |
1da177e4 LT |
352 | } else { |
353 | /* | |
354 | * Start of ag. Lookup the first inode chunk. | |
355 | */ | |
21875505 | 356 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp); |
1da177e4 LT |
357 | icount = 0; |
358 | } | |
359 | /* | |
360 | * Loop through inode btree records in this ag, | |
361 | * until we run out of inodes or space in the buffer. | |
362 | */ | |
363 | while (irbp < irbufend && icount < ubcount) { | |
2e287a73 CH |
364 | xfs_inobt_rec_incore_t r; |
365 | ||
1da177e4 LT |
366 | /* |
367 | * Loop as long as we're unable to read the | |
368 | * inode btree. | |
369 | */ | |
370 | while (error) { | |
371 | agino += XFS_INODES_PER_CHUNK; | |
372 | if (XFS_AGINO_TO_AGBNO(mp, agino) >= | |
16259e7d | 373 | be32_to_cpu(agi->agi_length)) |
1da177e4 | 374 | break; |
21875505 CH |
375 | error = xfs_inobt_lookup(cur, agino, |
376 | XFS_LOOKUP_GE, &tmp); | |
cd57e594 | 377 | cond_resched(); |
1da177e4 LT |
378 | } |
379 | /* | |
380 | * If ran off the end of the ag either with an error, | |
381 | * or the normal way, set end and stop collecting. | |
382 | */ | |
2e287a73 CH |
383 | if (error) { |
384 | end_of_ag = 1; | |
385 | break; | |
386 | } | |
387 | ||
388 | error = xfs_inobt_get_rec(cur, &r, &i); | |
389 | if (error || i == 0) { | |
1da177e4 LT |
390 | end_of_ag = 1; |
391 | break; | |
392 | } | |
2e287a73 | 393 | |
1da177e4 LT |
394 | /* |
395 | * If this chunk has any allocated inodes, save it. | |
26275093 | 396 | * Also start read-ahead now for this chunk. |
1da177e4 | 397 | */ |
2e287a73 | 398 | if (r.ir_freecount < XFS_INODES_PER_CHUNK) { |
26275093 NS |
399 | /* |
400 | * Loop over all clusters in the next chunk. | |
401 | * Do a readahead if there are any allocated | |
402 | * inodes in that cluster. | |
403 | */ | |
2e287a73 CH |
404 | agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino); |
405 | for (chunkidx = 0; | |
26275093 NS |
406 | chunkidx < XFS_INODES_PER_CHUNK; |
407 | chunkidx += nicluster, | |
408 | agbno += nbcluster) { | |
2e287a73 CH |
409 | if (xfs_inobt_maskn(chunkidx, nicluster) |
410 | & ~r.ir_free) | |
26275093 NS |
411 | xfs_btree_reada_bufs(mp, agno, |
412 | agbno, nbcluster); | |
413 | } | |
2e287a73 CH |
414 | irbp->ir_startino = r.ir_startino; |
415 | irbp->ir_freecount = r.ir_freecount; | |
416 | irbp->ir_free = r.ir_free; | |
1da177e4 | 417 | irbp++; |
2e287a73 | 418 | icount += XFS_INODES_PER_CHUNK - r.ir_freecount; |
1da177e4 LT |
419 | } |
420 | /* | |
421 | * Set agino to after this chunk and bump the cursor. | |
422 | */ | |
2e287a73 | 423 | agino = r.ir_startino + XFS_INODES_PER_CHUNK; |
637aa50f | 424 | error = xfs_btree_increment(cur, 0, &tmp); |
cd57e594 | 425 | cond_resched(); |
1da177e4 LT |
426 | } |
427 | /* | |
428 | * Drop the btree buffers and the agi buffer. | |
429 | * We can't hold any of the locks these represent | |
430 | * when calling iget. | |
431 | */ | |
432 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
433 | xfs_buf_relse(agbp); | |
434 | /* | |
435 | * Now format all the good inodes into the user's buffer. | |
436 | */ | |
437 | irbufend = irbp; | |
438 | for (irbp = irbuf; | |
cd57e594 | 439 | irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) { |
1da177e4 LT |
440 | /* |
441 | * Now process this chunk of inodes. | |
442 | */ | |
26275093 | 443 | for (agino = irbp->ir_startino, chunkidx = clustidx = 0; |
cd57e594 | 444 | XFS_BULKSTAT_UBLEFT(ubleft) && |
26275093 | 445 | irbp->ir_freecount < XFS_INODES_PER_CHUNK; |
1da177e4 LT |
446 | chunkidx++, clustidx++, agino++) { |
447 | ASSERT(chunkidx < XFS_INODES_PER_CHUNK); | |
448 | /* | |
449 | * Recompute agbno if this is the | |
450 | * first inode of the cluster. | |
451 | * | |
452 | * Careful with clustidx. There can be | |
9da096fd | 453 | * multiple clusters per chunk, a single |
1da177e4 LT |
454 | * cluster per chunk or a cluster that has |
455 | * inodes represented from several different | |
456 | * chunks (if blocksize is large). | |
457 | * | |
458 | * Because of this, the starting clustidx is | |
459 | * initialized to zero in this loop but must | |
460 | * later be reset after reading in the cluster | |
461 | * buffer. | |
462 | */ | |
463 | if ((chunkidx & (nicluster - 1)) == 0) { | |
464 | agbno = XFS_AGINO_TO_AGBNO(mp, | |
26275093 | 465 | irbp->ir_startino) + |
1da177e4 LT |
466 | ((chunkidx & nimask) >> |
467 | mp->m_sb.sb_inopblog); | |
1da177e4 | 468 | } |
c2cba57e LM |
469 | ino = XFS_AGINO_TO_INO(mp, agno, agino); |
470 | bno = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
1da177e4 LT |
471 | /* |
472 | * Skip if this inode is free. | |
473 | */ | |
c2cba57e LM |
474 | if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) { |
475 | lastino = ino; | |
1da177e4 | 476 | continue; |
c2cba57e | 477 | } |
1da177e4 LT |
478 | /* |
479 | * Count used inodes as free so we can tell | |
480 | * when the chunk is used up. | |
481 | */ | |
26275093 | 482 | irbp->ir_freecount++; |
1da177e4 LT |
483 | |
484 | /* | |
485 | * Get the inode and fill in a single buffer. | |
1da177e4 LT |
486 | */ |
487 | ubused = statstruct_size; | |
7dce11db CH |
488 | error = formatter(mp, ino, ubufp, ubleft, bno, |
489 | &ubused, &fmterror); | |
1da177e4 | 490 | if (fmterror == BULKSTAT_RV_NOTHING) { |
cd57e594 LM |
491 | if (error && error != ENOENT && |
492 | error != EINVAL) { | |
e132f54c | 493 | ubleft = 0; |
cd57e594 LM |
494 | rval = error; |
495 | break; | |
496 | } | |
497 | lastino = ino; | |
1da177e4 LT |
498 | continue; |
499 | } | |
500 | if (fmterror == BULKSTAT_RV_GIVEUP) { | |
501 | ubleft = 0; | |
502 | ASSERT(error); | |
503 | rval = error; | |
504 | break; | |
505 | } | |
506 | if (ubufp) | |
507 | ubufp += ubused; | |
508 | ubleft -= ubused; | |
509 | ubelem++; | |
510 | lastino = ino; | |
511 | } | |
cd57e594 LM |
512 | |
513 | cond_resched(); | |
1da177e4 LT |
514 | } |
515 | ||
516 | if (bp) | |
517 | xfs_buf_relse(bp); | |
518 | ||
519 | /* | |
520 | * Set up for the next loop iteration. | |
521 | */ | |
cd57e594 | 522 | if (XFS_BULKSTAT_UBLEFT(ubleft)) { |
1da177e4 LT |
523 | if (end_of_ag) { |
524 | agno++; | |
525 | agino = 0; | |
cd57e594 LM |
526 | } else |
527 | agino = XFS_INO_TO_AGINO(mp, lastino); | |
1da177e4 LT |
528 | } else |
529 | break; | |
530 | } | |
531 | /* | |
532 | * Done, we're either out of filesystem or space to put the data. | |
533 | */ | |
bdfb0430 | 534 | kmem_free_large(irbuf); |
1da177e4 | 535 | *ubcountp = ubelem; |
cd57e594 LM |
536 | /* |
537 | * Found some inodes, return them now and return the error next time. | |
538 | */ | |
539 | if (ubelem) | |
540 | rval = 0; | |
1da177e4 LT |
541 | if (agno >= mp->m_sb.sb_agcount) { |
542 | /* | |
543 | * If we ran out of filesystem, mark lastino as off | |
544 | * the end of the filesystem, so the next call | |
545 | * will return immediately. | |
546 | */ | |
547 | *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0); | |
548 | *done = 1; | |
549 | } else | |
550 | *lastinop = (xfs_ino_t)lastino; | |
551 | ||
552 | return rval; | |
553 | } | |
554 | ||
555 | /* | |
556 | * Return stat information in bulk (by-inode) for the filesystem. | |
557 | * Special case for non-sequential one inode bulkstat. | |
558 | */ | |
559 | int /* error status */ | |
560 | xfs_bulkstat_single( | |
561 | xfs_mount_t *mp, /* mount point for filesystem */ | |
562 | xfs_ino_t *lastinop, /* inode to return */ | |
563 | char __user *buffer, /* buffer with inode stats */ | |
c41564b5 | 564 | int *done) /* 1 if there are more stats to get */ |
1da177e4 LT |
565 | { |
566 | int count; /* count value for bulkstat call */ | |
567 | int error; /* return value */ | |
568 | xfs_ino_t ino; /* filesystem inode number */ | |
569 | int res; /* result from bs1 */ | |
570 | ||
571 | /* | |
572 | * note that requesting valid inode numbers which are not allocated | |
573 | * to inodes will most likely cause xfs_itobp to generate warning | |
574 | * messages about bad magic numbers. This is ok. The fact that | |
575 | * the inode isn't actually an inode is handled by the | |
576 | * error check below. Done this way to make the usual case faster | |
577 | * at the expense of the error case. | |
578 | */ | |
579 | ||
580 | ino = (xfs_ino_t)*lastinop; | |
581 | error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), | |
7dce11db | 582 | 0, NULL, &res); |
1da177e4 LT |
583 | if (error) { |
584 | /* | |
585 | * Special case way failed, do it the "long" way | |
586 | * to see if that works. | |
587 | */ | |
588 | (*lastinop)--; | |
589 | count = 1; | |
590 | if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one, | |
7dce11db | 591 | sizeof(xfs_bstat_t), buffer, done)) |
1da177e4 LT |
592 | return error; |
593 | if (count == 0 || (xfs_ino_t)*lastinop != ino) | |
594 | return error == EFSCORRUPTED ? | |
595 | XFS_ERROR(EINVAL) : error; | |
596 | else | |
597 | return 0; | |
598 | } | |
599 | *done = 0; | |
600 | return 0; | |
601 | } | |
602 | ||
faa63e95 MM |
603 | int |
604 | xfs_inumbers_fmt( | |
605 | void __user *ubuffer, /* buffer to write to */ | |
606 | const xfs_inogrp_t *buffer, /* buffer to read from */ | |
607 | long count, /* # of elements to read */ | |
608 | long *written) /* # of bytes written */ | |
609 | { | |
610 | if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer))) | |
611 | return -EFAULT; | |
612 | *written = count * sizeof(*buffer); | |
613 | return 0; | |
614 | } | |
615 | ||
1da177e4 LT |
616 | /* |
617 | * Return inode number table for the filesystem. | |
618 | */ | |
619 | int /* error status */ | |
620 | xfs_inumbers( | |
621 | xfs_mount_t *mp, /* mount point for filesystem */ | |
622 | xfs_ino_t *lastino, /* last inode returned */ | |
623 | int *count, /* size of buffer/count returned */ | |
faa63e95 MM |
624 | void __user *ubuffer,/* buffer with inode descriptions */ |
625 | inumbers_fmt_pf formatter) | |
1da177e4 LT |
626 | { |
627 | xfs_buf_t *agbp; | |
628 | xfs_agino_t agino; | |
629 | xfs_agnumber_t agno; | |
630 | int bcount; | |
631 | xfs_inogrp_t *buffer; | |
632 | int bufidx; | |
633 | xfs_btree_cur_t *cur; | |
634 | int error; | |
2e287a73 | 635 | xfs_inobt_rec_incore_t r; |
1da177e4 LT |
636 | int i; |
637 | xfs_ino_t ino; | |
638 | int left; | |
639 | int tmp; | |
640 | ||
641 | ino = (xfs_ino_t)*lastino; | |
642 | agno = XFS_INO_TO_AGNO(mp, ino); | |
643 | agino = XFS_INO_TO_AGINO(mp, ino); | |
644 | left = *count; | |
645 | *count = 0; | |
e6a4b37f | 646 | bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer))); |
1da177e4 LT |
647 | buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP); |
648 | error = bufidx = 0; | |
649 | cur = NULL; | |
650 | agbp = NULL; | |
651 | while (left > 0 && agno < mp->m_sb.sb_agcount) { | |
652 | if (agbp == NULL) { | |
1da177e4 | 653 | error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); |
1da177e4 LT |
654 | if (error) { |
655 | /* | |
656 | * If we can't read the AGI of this ag, | |
657 | * then just skip to the next one. | |
658 | */ | |
659 | ASSERT(cur == NULL); | |
660 | agbp = NULL; | |
661 | agno++; | |
662 | agino = 0; | |
663 | continue; | |
664 | } | |
561f7d17 | 665 | cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno); |
21875505 CH |
666 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE, |
667 | &tmp); | |
1da177e4 LT |
668 | if (error) { |
669 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
670 | cur = NULL; | |
671 | xfs_buf_relse(agbp); | |
672 | agbp = NULL; | |
673 | /* | |
59c51591 | 674 | * Move up the last inode in the current |
1da177e4 LT |
675 | * chunk. The lookup_ge will always get |
676 | * us the first inode in the next chunk. | |
677 | */ | |
678 | agino += XFS_INODES_PER_CHUNK - 1; | |
679 | continue; | |
680 | } | |
681 | } | |
2e287a73 CH |
682 | error = xfs_inobt_get_rec(cur, &r, &i); |
683 | if (error || i == 0) { | |
1da177e4 LT |
684 | xfs_buf_relse(agbp); |
685 | agbp = NULL; | |
686 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
687 | cur = NULL; | |
688 | agno++; | |
689 | agino = 0; | |
690 | continue; | |
691 | } | |
2e287a73 CH |
692 | agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1; |
693 | buffer[bufidx].xi_startino = | |
694 | XFS_AGINO_TO_INO(mp, agno, r.ir_startino); | |
695 | buffer[bufidx].xi_alloccount = | |
696 | XFS_INODES_PER_CHUNK - r.ir_freecount; | |
697 | buffer[bufidx].xi_allocmask = ~r.ir_free; | |
1da177e4 LT |
698 | bufidx++; |
699 | left--; | |
700 | if (bufidx == bcount) { | |
faa63e95 MM |
701 | long written; |
702 | if (formatter(ubuffer, buffer, bufidx, &written)) { | |
1da177e4 LT |
703 | error = XFS_ERROR(EFAULT); |
704 | break; | |
705 | } | |
faa63e95 | 706 | ubuffer += written; |
1da177e4 LT |
707 | *count += bufidx; |
708 | bufidx = 0; | |
709 | } | |
710 | if (left) { | |
637aa50f | 711 | error = xfs_btree_increment(cur, 0, &tmp); |
1da177e4 LT |
712 | if (error) { |
713 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
714 | cur = NULL; | |
715 | xfs_buf_relse(agbp); | |
716 | agbp = NULL; | |
717 | /* | |
718 | * The agino value has already been bumped. | |
719 | * Just try to skip up to it. | |
720 | */ | |
721 | agino += XFS_INODES_PER_CHUNK; | |
722 | continue; | |
723 | } | |
724 | } | |
725 | } | |
726 | if (!error) { | |
727 | if (bufidx) { | |
faa63e95 MM |
728 | long written; |
729 | if (formatter(ubuffer, buffer, bufidx, &written)) | |
1da177e4 LT |
730 | error = XFS_ERROR(EFAULT); |
731 | else | |
732 | *count += bufidx; | |
733 | } | |
734 | *lastino = XFS_AGINO_TO_INO(mp, agno, agino); | |
735 | } | |
f0e2d93c | 736 | kmem_free(buffer); |
1da177e4 LT |
737 | if (cur) |
738 | xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR : | |
739 | XFS_BTREE_NOERROR)); | |
740 | if (agbp) | |
741 | xfs_buf_relse(agbp); | |
742 | return error; | |
743 | } |