blob: d583105144233182291f4b30039e45b7024d8196 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110026#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_ialloc.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110028#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_itable.h"
30#include "xfs_error.h"
Christoph Hellwigf2d67612010-06-24 11:52:50 +100031#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110032#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100034/*
35 * Return stat information for one inode.
36 * Return 0 if ok, else errno.
37 */
38int
39xfs_bulkstat_one_int(
40 struct xfs_mount *mp, /* mount point for filesystem */
41 xfs_ino_t ino, /* inode to get data for */
42 void __user *buffer, /* buffer to place output in */
43 int ubsize, /* size of buffer */
44 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100045 int *ubused, /* bytes used by me */
46 int *stat) /* BULKSTAT_RV_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100048 struct xfs_icdinode *dic; /* dinode core info pointer */
49 struct xfs_inode *ip; /* incore inode pointer */
Dave Chinner39878482016-02-09 16:54:58 +110050 struct inode *inode;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100051 struct xfs_bstat *buf; /* return buffer */
52 int error = 0; /* error value */
53
54 *stat = BULKSTAT_RV_NOTHING;
55
56 if (!buffer || xfs_internal_inum(mp, ino))
Dave Chinner24513372014-06-25 14:58:08 +100057 return -EINVAL;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100058
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070059 buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100060 if (!buf)
Dave Chinner24513372014-06-25 14:58:08 +100061 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Nathan Scott745b1f472006-09-28 11:02:23 +100063 error = xfs_iget(mp, NULL, ino,
Dave Chinner5132ba82012-03-22 05:15:10 +000064 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
65 XFS_ILOCK_SHARED, &ip);
Jie Liu8fe65772014-07-24 11:33:28 +100066 if (error)
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100067 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110070 ASSERT(ip->i_imap.im_blkno != 0);
Dave Chinner39878482016-02-09 16:54:58 +110071 inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 dic = &ip->i_d;
74
75 /* xfs_iget returns the following without needing
76 * further change.
77 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +000078 buf->bs_projid_lo = dic->di_projid_lo;
79 buf->bs_projid_hi = dic->di_projid_hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 buf->bs_ino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 buf->bs_uid = dic->di_uid;
82 buf->bs_gid = dic->di_gid;
83 buf->bs_size = dic->di_size;
Dave Chinner39878482016-02-09 16:54:58 +110084
Dave Chinner54d7b5c2016-02-09 16:54:58 +110085 buf->bs_nlink = inode->i_nlink;
Dave Chinner39878482016-02-09 16:54:58 +110086 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
87 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
88 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
89 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
90 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
91 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
Dave Chinner9e9a2672016-02-09 16:54:58 +110092 buf->bs_gen = inode->i_generation;
Dave Chinnerc19b3b052016-02-09 16:54:58 +110093 buf->bs_mode = inode->i_mode;
Dave Chinner39878482016-02-09 16:54:58 +110094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 buf->bs_xflags = xfs_ip2xflags(ip);
96 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
97 buf->bs_extents = dic->di_nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
99 buf->bs_dmevmask = dic->di_dmevmask;
100 buf->bs_dmstate = dic->di_dmstate;
101 buf->bs_aextents = dic->di_anextents;
Dave Chinner07000ee2010-03-05 04:41:14 +0000102 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700104 if (dic->di_version == 3) {
105 if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
106 buf->bs_cowextsize = dic->di_cowextsize <<
107 mp->m_sb.sb_blocklog;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 switch (dic->di_format) {
111 case XFS_DINODE_FMT_DEV:
Christoph Hellwig66f36462017-10-19 11:07:09 -0700112 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 buf->bs_blksize = BLKDEV_IOSIZE;
114 buf->bs_blocks = 0;
115 break;
116 case XFS_DINODE_FMT_LOCAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 buf->bs_rdev = 0;
118 buf->bs_blksize = mp->m_sb.sb_blocksize;
119 buf->bs_blocks = 0;
120 break;
121 case XFS_DINODE_FMT_EXTENTS:
122 case XFS_DINODE_FMT_BTREE:
123 buf->bs_rdev = 0;
124 buf->bs_blksize = mp->m_sb.sb_blocksize;
125 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
126 break;
127 }
Christoph Hellwigf2d67612010-06-24 11:52:50 +1000128 xfs_iunlock(ip, XFS_ILOCK_SHARED);
129 IRELE(ip);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000130
131 error = formatter(buffer, ubsize, ubused, buf);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000132 if (!error)
133 *stat = BULKSTAT_RV_DIDONE;
134
135 out_free:
136 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return error;
138}
139
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600140/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000141STATIC int
142xfs_bulkstat_one_fmt(
143 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600144 int ubsize,
145 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000146 const xfs_bstat_t *buffer)
147{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600148 if (ubsize < sizeof(*buffer))
Dave Chinner24513372014-06-25 14:58:08 +1000149 return -ENOMEM;
Michal Marekfaa63e92007-07-11 11:10:19 +1000150 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
Dave Chinner24513372014-06-25 14:58:08 +1000151 return -EFAULT;
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600152 if (ubused)
153 *ubused = sizeof(*buffer);
154 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000155}
156
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600157int
158xfs_bulkstat_one(
159 xfs_mount_t *mp, /* mount point for filesystem */
160 xfs_ino_t ino, /* inode number to get data for */
161 void __user *buffer, /* buffer to place output in */
162 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600163 int *ubused, /* bytes used by me */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600164 int *stat) /* BULKSTAT_RV_... */
165{
166 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000167 xfs_bulkstat_one_fmt, ubused, stat);
Nathan Scott8b56f082006-09-28 11:01:46 +1000168}
169
Jie Liu4b8fdfe2014-07-24 18:41:18 +1000170/*
171 * Loop over all clusters in a chunk for a given incore inode allocation btree
172 * record. Do a readahead if there are any allocated inodes in that cluster.
173 */
174STATIC void
175xfs_bulkstat_ichunk_ra(
176 struct xfs_mount *mp,
177 xfs_agnumber_t agno,
178 struct xfs_inobt_rec_incore *irec)
179{
180 xfs_agblock_t agbno;
181 struct blk_plug plug;
182 int blks_per_cluster;
183 int inodes_per_cluster;
184 int i; /* inode chunk index */
185
186 agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
187 blks_per_cluster = xfs_icluster_size_fsb(mp);
188 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
189
190 blk_start_plug(&plug);
191 for (i = 0; i < XFS_INODES_PER_CHUNK;
192 i += inodes_per_cluster, agbno += blks_per_cluster) {
193 if (xfs_inobt_maskn(i, inodes_per_cluster) & ~irec->ir_free) {
194 xfs_btree_reada_bufs(mp, agno, agbno, blks_per_cluster,
195 &xfs_inode_buf_ops);
196 }
197 }
198 blk_finish_plug(&plug);
199}
200
Jie Liuf3d1e582014-07-24 18:42:21 +1000201/*
202 * Lookup the inode chunk that the given inode lives in and then get the record
203 * if we found the chunk. If the inode was not the last in the chunk and there
204 * are some left allocated, update the data for the pointed-to record as well as
205 * return the count of grabbed inodes.
206 */
207STATIC int
208xfs_bulkstat_grab_ichunk(
209 struct xfs_btree_cur *cur, /* btree cursor */
210 xfs_agino_t agino, /* starting inode of chunk */
211 int *icount,/* return # of inodes grabbed */
212 struct xfs_inobt_rec_incore *irec) /* btree record */
213{
214 int idx; /* index into inode chunk */
215 int stat;
216 int error = 0;
217
218 /* Lookup the inode chunk that this inode lives in */
219 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
220 if (error)
221 return error;
222 if (!stat) {
223 *icount = 0;
224 return error;
225 }
226
227 /* Get the record, should always work */
228 error = xfs_inobt_get_rec(cur, irec, &stat);
229 if (error)
230 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100231 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1);
Jie Liuf3d1e582014-07-24 18:42:21 +1000232
233 /* Check if the record contains the inode in request */
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100234 if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) {
235 *icount = 0;
236 return 0;
237 }
Jie Liuf3d1e582014-07-24 18:42:21 +1000238
239 idx = agino - irec->ir_startino + 1;
240 if (idx < XFS_INODES_PER_CHUNK &&
241 (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
242 int i;
243
244 /* We got a right chunk with some left inodes allocated at it.
245 * Grab the chunk record. Mark all the uninteresting inodes
246 * free -- because they're before our start point.
247 */
248 for (i = 0; i < idx; i++) {
249 if (XFS_INOBT_MASK(i) & ~irec->ir_free)
250 irec->ir_freecount++;
251 }
252
253 irec->ir_free |= xfs_inobt_maskn(0, idx);
Brian Foster12d07142015-05-29 09:04:19 +1000254 *icount = irec->ir_count - irec->ir_freecount;
Jie Liuf3d1e582014-07-24 18:42:21 +1000255 }
256
257 return 0;
258}
259
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100260#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
261
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100262struct xfs_bulkstat_agichunk {
263 char __user **ac_ubuffer;/* pointer into user's buffer */
264 int ac_ubleft; /* bytes left in user's buffer */
265 int ac_ubelem; /* spaces used in user's buffer */
266};
267
Nathan Scott8b56f082006-09-28 11:01:46 +1000268/*
Jie Liu1e773c42014-08-04 11:22:31 +1000269 * Process inodes in chunk with a pointer to a formatter function
270 * that will iget the inode and fill in the appropriate structure.
271 */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100272static int
Jie Liu1e773c42014-08-04 11:22:31 +1000273xfs_bulkstat_ag_ichunk(
274 struct xfs_mount *mp,
275 xfs_agnumber_t agno,
276 struct xfs_inobt_rec_incore *irbp,
277 bulkstat_one_pf formatter,
278 size_t statstruct_size,
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100279 struct xfs_bulkstat_agichunk *acp,
Dave Chinner00275892014-11-07 08:33:52 +1100280 xfs_agino_t *last_agino)
Jie Liu1e773c42014-08-04 11:22:31 +1000281{
Jie Liu1e773c42014-08-04 11:22:31 +1000282 char __user **ubufp = acp->ac_ubuffer;
Dave Chinner2b831ac62014-11-07 08:30:58 +1100283 int chunkidx;
Jie Liu1e773c42014-08-04 11:22:31 +1000284 int error = 0;
Dave Chinner00275892014-11-07 08:33:52 +1100285 xfs_agino_t agino = irbp->ir_startino;
Jie Liu1e773c42014-08-04 11:22:31 +1000286
Dave Chinner2b831ac62014-11-07 08:30:58 +1100287 for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK;
288 chunkidx++, agino++) {
289 int fmterror;
Jie Liu1e773c42014-08-04 11:22:31 +1000290 int ubused;
Dave Chinner00275892014-11-07 08:33:52 +1100291
292 /* inode won't fit in buffer, we are done */
293 if (acp->ac_ubleft < statstruct_size)
294 break;
Jie Liu1e773c42014-08-04 11:22:31 +1000295
Jie Liu1e773c42014-08-04 11:22:31 +1000296 /* Skip if this inode is free */
Dave Chinner00275892014-11-07 08:33:52 +1100297 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
Jie Liu1e773c42014-08-04 11:22:31 +1000298 continue;
Jie Liu1e773c42014-08-04 11:22:31 +1000299
Jie Liu1e773c42014-08-04 11:22:31 +1000300 /* Get the inode and fill in a single buffer */
301 ubused = statstruct_size;
Dave Chinner00275892014-11-07 08:33:52 +1100302 error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino),
303 *ubufp, acp->ac_ubleft, &ubused, &fmterror);
304
Dave Chinner2b831ac62014-11-07 08:30:58 +1100305 if (fmterror == BULKSTAT_RV_GIVEUP ||
306 (error && error != -ENOENT && error != -EINVAL)) {
307 acp->ac_ubleft = 0;
Jie Liu1e773c42014-08-04 11:22:31 +1000308 ASSERT(error);
309 break;
310 }
Jie Liu1e773c42014-08-04 11:22:31 +1000311
Dave Chinner2b831ac62014-11-07 08:30:58 +1100312 /* be careful not to leak error if at end of chunk */
313 if (fmterror == BULKSTAT_RV_NOTHING || error) {
Dave Chinner2b831ac62014-11-07 08:30:58 +1100314 error = 0;
315 continue;
316 }
317
318 *ubufp += ubused;
319 acp->ac_ubleft -= ubused;
320 acp->ac_ubelem++;
Dave Chinner2b831ac62014-11-07 08:30:58 +1100321 }
Jie Liu1e773c42014-08-04 11:22:31 +1000322
Dave Chinner00275892014-11-07 08:33:52 +1100323 /*
324 * Post-update *last_agino. At this point, agino will always point one
325 * inode past the last inode we processed successfully. Hence we
326 * substract that inode when setting the *last_agino cursor so that we
327 * return the correct cookie to userspace. On the next bulkstat call,
328 * the inode under the lastino cookie will be skipped as we have already
329 * processed it here.
330 */
331 *last_agino = agino - 1;
332
Jie Liu1e773c42014-08-04 11:22:31 +1000333 return error;
334}
335
336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Return stat information in bulk (by-inode) for the filesystem.
338 */
339int /* error status */
340xfs_bulkstat(
341 xfs_mount_t *mp, /* mount point for filesystem */
342 xfs_ino_t *lastinop, /* last inode returned */
343 int *ubcountp, /* size of buffer/count returned */
344 bulkstat_one_pf formatter, /* func that'd fill a single buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 size_t statstruct_size, /* sizeof struct filling */
346 char __user *ubuffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000347 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 xfs_buf_t *agbp; /* agi header buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_agino_t agino; /* inode # in allocation group */
351 xfs_agnumber_t agno; /* allocation group number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
Nathan Scott262750932006-09-28 11:02:03 +1000353 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int nirbuf; /* size of irbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int ubcount; /* size of user's buffer */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100356 struct xfs_bulkstat_agichunk ac;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100357 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * Get the last inode value, see if there's nothing to do.
361 */
Dave Chinner00275892014-11-07 08:33:52 +1100362 agno = XFS_INO_TO_AGNO(mp, *lastinop);
363 agino = XFS_INO_TO_AGINO(mp, *lastinop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (agno >= mp->m_sb.sb_agcount ||
Dave Chinner00275892014-11-07 08:33:52 +1100365 *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *done = 1;
367 *ubcountp = 0;
368 return 0;
369 }
Jie Liu296dfd72014-07-24 18:40:26 +1000370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ubcount = *ubcountp; /* statstruct's */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100372 ac.ac_ubuffer = &ubuffer;
373 ac.ac_ubleft = ubcount * statstruct_size; /* bytes */;
374 ac.ac_ubelem = 0;
375
376 *ubcountp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *done = 0;
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100378
Darrick J. Wong08b005f2017-03-06 11:58:20 -0800379 irbuf = kmem_zalloc_large(PAGE_SIZE * 4, KM_SLEEP);
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000380 if (!irbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000381 return -ENOMEM;
Darrick J. Wong08b005f2017-03-06 11:58:20 -0800382 nirbuf = (PAGE_SIZE * 4) / sizeof(*irbuf);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * Loop over the allocation groups, starting from the last
386 * inode returned; 0 means start of the allocation group.
387 */
Dave Chinner6e57c5422014-11-07 08:31:13 +1100388 while (agno < mp->m_sb.sb_agcount) {
389 struct xfs_inobt_rec_incore *irbp = irbuf;
390 struct xfs_inobt_rec_incore *irbufend = irbuf + nirbuf;
391 bool end_of_ag = false;
392 int icount = 0;
393 int stat;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Jie Liud4c27342014-07-24 18:40:43 +1000396 if (error)
397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
399 * Allocate and initialize a btree cursor for ialloc btree.
400 */
Brian Foster57bd3db2014-04-24 16:00:50 +1000401 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
402 XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (agino > 0) {
404 /*
Jie Liuf3d1e582014-07-24 18:42:21 +1000405 * In the middle of an allocation group, we need to get
406 * the remainder of the chunk we're in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Jie Liuf3d1e582014-07-24 18:42:21 +1000408 struct xfs_inobt_rec_incore r;
409
410 error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
411 if (error)
Dave Chinnera6bbce52014-10-29 08:22:18 +1100412 goto del_cursor;
Jie Liuf3d1e582014-07-24 18:42:21 +1000413 if (icount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300414 irbp->ir_startino = r.ir_startino;
Brian Foster12d07142015-05-29 09:04:19 +1000415 irbp->ir_holemask = r.ir_holemask;
416 irbp->ir_count = r.ir_count;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300417 irbp->ir_freecount = r.ir_freecount;
418 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 irbp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Jie Liuf3d1e582014-07-24 18:42:21 +1000421 /* Increment to the next record */
Dave Chinnerafa947c2014-11-07 08:29:57 +1100422 error = xfs_btree_increment(cur, 0, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } else {
Jie Liuf3d1e582014-07-24 18:42:21 +1000424 /* Start of ag. Lookup the first inode chunk */
Dave Chinnerafa947c2014-11-07 08:29:57 +1100425 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Dave Chinnerafa947c2014-11-07 08:29:57 +1100427 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100428 end_of_ag = true;
Dave Chinnera6bbce52014-10-29 08:22:18 +1100429 goto del_cursor;
Dave Chinnerafa947c2014-11-07 08:29:57 +1100430 }
Jie Liud4c27342014-07-24 18:40:43 +1000431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
433 * Loop through inode btree records in this ag,
434 * until we run out of inodes or space in the buffer.
435 */
436 while (irbp < irbufend && icount < ubcount) {
Jie Liud4c27342014-07-24 18:40:43 +1000437 struct xfs_inobt_rec_incore r;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300438
Dave Chinnerafa947c2014-11-07 08:29:57 +1100439 error = xfs_inobt_get_rec(cur, &r, &stat);
440 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100441 end_of_ag = true;
Dave Chinnera6bbce52014-10-29 08:22:18 +1100442 goto del_cursor;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300443 }
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000447 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Brian Foster12d07142015-05-29 09:04:19 +1000449 if (r.ir_freecount < r.ir_count) {
Jie Liu4b8fdfe2014-07-24 18:41:18 +1000450 xfs_bulkstat_ichunk_ra(mp, agno, &r);
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300451 irbp->ir_startino = r.ir_startino;
Brian Foster12d07142015-05-29 09:04:19 +1000452 irbp->ir_holemask = r.ir_holemask;
453 irbp->ir_count = r.ir_count;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300454 irbp->ir_freecount = r.ir_freecount;
455 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 irbp++;
Brian Foster12d07142015-05-29 09:04:19 +1000457 icount += r.ir_count - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Dave Chinnerafa947c2014-11-07 08:29:57 +1100459 error = xfs_btree_increment(cur, 0, &stat);
460 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100461 end_of_ag = true;
Jan Kara7a19dee2014-10-30 10:34:52 +1100462 goto del_cursor;
463 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100464 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Dave Chinnera6bbce52014-10-29 08:22:18 +1100466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
Dave Chinnera6bbce52014-10-29 08:22:18 +1100468 * Drop the btree buffers and the agi buffer as we can't hold any
469 * of the locks these represent when calling iget. If there is a
470 * pending error, then we are done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Dave Chinnera6bbce52014-10-29 08:22:18 +1100472del_cursor:
Brian Fosterf3070802015-08-19 10:00:53 +1000473 xfs_btree_del_cursor(cur, error ?
474 XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 xfs_buf_relse(agbp);
Dave Chinnera6bbce52014-10-29 08:22:18 +1100476 if (error)
477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
Dave Chinner00275892014-11-07 08:33:52 +1100479 * Now format all the good inodes into the user's buffer. The
480 * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer
481 * for the next loop iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483 irbufend = irbp;
484 for (irbp = irbuf;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100485 irbp < irbufend && ac.ac_ubleft >= statstruct_size;
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100486 irbp++) {
Jie Liu1e773c42014-08-04 11:22:31 +1000487 error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100488 formatter, statstruct_size, &ac,
Dave Chinner00275892014-11-07 08:33:52 +1100489 &agino);
Jie Liu1e773c42014-08-04 11:22:31 +1000490 if (error)
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100493 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100495
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100496 /*
497 * If we've run out of space or had a formatting error, we
498 * are now done
499 */
500 if (ac.ac_ubleft < statstruct_size || error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100502
503 if (end_of_ag) {
504 agno++;
505 agino = 0;
Dave Chinner00275892014-11-07 08:33:52 +1100506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 /*
509 * Done, we're either out of filesystem or space to put the data.
510 */
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000511 kmem_free(irbuf);
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100512 *ubcountp = ac.ac_ubelem;
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100513
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100514 /*
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100515 * We found some inodes, so clear the error status and return them.
516 * The lastino pointer will point directly at the inode that triggered
517 * any error that occurred, so on the next call the error will be
518 * triggered again and propagated to userspace as there will be no
519 * formatted inodes in the buffer.
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100520 */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100521 if (ac.ac_ubelem)
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100522 error = 0;
523
Dave Chinner00275892014-11-07 08:33:52 +1100524 /*
525 * If we ran out of filesystem, lastino will point off the end of
526 * the filesystem so the next call will return immediately.
527 */
528 *lastinop = XFS_AGINO_TO_INO(mp, agno, agino);
529 if (agno >= mp->m_sb.sb_agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100532 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Michal Marekfaa63e92007-07-11 11:10:19 +1000535int
536xfs_inumbers_fmt(
537 void __user *ubuffer, /* buffer to write to */
Jie Liu549fa002014-07-24 12:11:47 +1000538 const struct xfs_inogrp *buffer, /* buffer to read from */
Michal Marekfaa63e92007-07-11 11:10:19 +1000539 long count, /* # of elements to read */
540 long *written) /* # of bytes written */
541{
542 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
543 return -EFAULT;
544 *written = count * sizeof(*buffer);
545 return 0;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * Return inode number table for the filesystem.
550 */
551int /* error status */
552xfs_inumbers(
Jie Liu549fa002014-07-24 12:11:47 +1000553 struct xfs_mount *mp,/* mount point for filesystem */
554 xfs_ino_t *lastino,/* last inode returned */
555 int *count,/* size of buffer/count returned */
556 void __user *ubuffer,/* buffer with inode descriptions */
557 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jie Liu549fa002014-07-24 12:11:47 +1000559 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, *lastino);
560 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, *lastino);
561 struct xfs_btree_cur *cur = NULL;
Jie Liuc7cb51d2014-07-24 12:18:47 +1000562 struct xfs_buf *agbp = NULL;
Jie Liu549fa002014-07-24 12:11:47 +1000563 struct xfs_inogrp *buffer;
564 int bcount;
565 int left = *count;
566 int bufidx = 0;
567 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 *count = 0;
Jie Liu549fa002014-07-24 12:11:47 +1000570 if (agno >= mp->m_sb.sb_agcount ||
571 *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
572 return error;
573
Tim Shimmine6a4b372007-11-23 16:30:42 +1100574 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Darrick J. Wongbf9216f2017-04-03 12:22:39 -0700575 buffer = kmem_zalloc(bcount * sizeof(*buffer), KM_SLEEP);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000576 do {
Jie Liu549fa002014-07-24 12:11:47 +1000577 struct xfs_inobt_rec_incore r;
578 int stat;
579
Jie Liuc7cb51d2014-07-24 12:18:47 +1000580 if (!agbp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000582 if (error)
583 break;
584
Brian Foster57bd3db2014-04-24 16:00:50 +1000585 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
586 XFS_BTNUM_INO);
Christoph Hellwig21875502009-08-31 20:58:21 -0300587 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
Jie Liu549fa002014-07-24 12:11:47 +1000588 &stat);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000589 if (error)
590 break;
591 if (!stat)
592 goto next_ag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000594
Jie Liu549fa002014-07-24 12:11:47 +1000595 error = xfs_inobt_get_rec(cur, &r, &stat);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000596 if (error)
597 break;
598 if (!stat)
599 goto next_ag;
600
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300601 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
602 buffer[bufidx].xi_startino =
603 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
Brian Foster12d07142015-05-29 09:04:19 +1000604 buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300605 buffer[bufidx].xi_allocmask = ~r.ir_free;
Jie Liuc7cb51d2014-07-24 12:18:47 +1000606 if (++bufidx == bcount) {
607 long written;
608
Jie Liu549fa002014-07-24 12:11:47 +1000609 error = formatter(ubuffer, buffer, bufidx, &written);
610 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
Michal Marekfaa63e92007-07-11 11:10:19 +1000612 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *count += bufidx;
614 bufidx = 0;
615 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000616 if (!--left)
617 break;
618
619 error = xfs_btree_increment(cur, 0, &stat);
620 if (error)
621 break;
622 if (stat)
623 continue;
624
625next_ag:
626 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
627 cur = NULL;
628 xfs_buf_relse(agbp);
629 agbp = NULL;
630 agino = 0;
Eric Sandeena8b1ee82014-10-13 10:21:53 +1100631 agno++;
632 } while (agno < mp->m_sb.sb_agcount);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!error) {
635 if (bufidx) {
Jie Liuc7cb51d2014-07-24 12:18:47 +1000636 long written;
637
Jie Liu549fa002014-07-24 12:11:47 +1000638 error = formatter(ubuffer, buffer, bufidx, &written);
639 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *count += bufidx;
641 }
642 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
643 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000644
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000645 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (cur)
647 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
648 XFS_BTREE_NOERROR));
649 if (agbp)
650 xfs_buf_relse(agbp);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return error;
653}