blob: e9508ba01ed19973f4e7493156c11998a8be255d [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110014#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_ialloc.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110016#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_itable.h"
18#include "xfs_error.h"
Christoph Hellwigf2d67612010-06-24 11:52:50 +100019#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110020#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100022/*
23 * Return stat information for one inode.
24 * Return 0 if ok, else errno.
25 */
26int
27xfs_bulkstat_one_int(
28 struct xfs_mount *mp, /* mount point for filesystem */
29 xfs_ino_t ino, /* inode to get data for */
30 void __user *buffer, /* buffer to place output in */
31 int ubsize, /* size of buffer */
32 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100033 int *ubused, /* bytes used by me */
34 int *stat) /* BULKSTAT_RV_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100036 struct xfs_icdinode *dic; /* dinode core info pointer */
37 struct xfs_inode *ip; /* incore inode pointer */
Dave Chinner39878482016-02-09 16:54:58 +110038 struct inode *inode;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100039 struct xfs_bstat *buf; /* return buffer */
40 int error = 0; /* error value */
41
42 *stat = BULKSTAT_RV_NOTHING;
43
44 if (!buffer || xfs_internal_inum(mp, ino))
Dave Chinner24513372014-06-25 14:58:08 +100045 return -EINVAL;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100046
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070047 buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100048 if (!buf)
Dave Chinner24513372014-06-25 14:58:08 +100049 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nathan Scott745b1f472006-09-28 11:02:23 +100051 error = xfs_iget(mp, NULL, ino,
Dave Chinner5132ba82012-03-22 05:15:10 +000052 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
53 XFS_ILOCK_SHARED, &ip);
Jie Liu8fe65772014-07-24 11:33:28 +100054 if (error)
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100055 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110058 ASSERT(ip->i_imap.im_blkno != 0);
Dave Chinner39878482016-02-09 16:54:58 +110059 inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 dic = &ip->i_d;
62
63 /* xfs_iget returns the following without needing
64 * further change.
65 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +000066 buf->bs_projid_lo = dic->di_projid_lo;
67 buf->bs_projid_hi = dic->di_projid_hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 buf->bs_ino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 buf->bs_uid = dic->di_uid;
70 buf->bs_gid = dic->di_gid;
71 buf->bs_size = dic->di_size;
Dave Chinner39878482016-02-09 16:54:58 +110072
Dave Chinner54d7b5c2016-02-09 16:54:58 +110073 buf->bs_nlink = inode->i_nlink;
Dave Chinner39878482016-02-09 16:54:58 +110074 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
75 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
76 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
77 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
78 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
79 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
Dave Chinner9e9a2672016-02-09 16:54:58 +110080 buf->bs_gen = inode->i_generation;
Dave Chinnerc19b3b052016-02-09 16:54:58 +110081 buf->bs_mode = inode->i_mode;
Dave Chinner39878482016-02-09 16:54:58 +110082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 buf->bs_xflags = xfs_ip2xflags(ip);
84 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
85 buf->bs_extents = dic->di_nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
87 buf->bs_dmevmask = dic->di_dmevmask;
88 buf->bs_dmstate = dic->di_dmstate;
89 buf->bs_aextents = dic->di_anextents;
Dave Chinner07000ee2010-03-05 04:41:14 +000090 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070092 if (dic->di_version == 3) {
93 if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
94 buf->bs_cowextsize = dic->di_cowextsize <<
95 mp->m_sb.sb_blocklog;
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 switch (dic->di_format) {
99 case XFS_DINODE_FMT_DEV:
Christoph Hellwig66f36462017-10-19 11:07:09 -0700100 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 buf->bs_blksize = BLKDEV_IOSIZE;
102 buf->bs_blocks = 0;
103 break;
104 case XFS_DINODE_FMT_LOCAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 buf->bs_rdev = 0;
106 buf->bs_blksize = mp->m_sb.sb_blocksize;
107 buf->bs_blocks = 0;
108 break;
109 case XFS_DINODE_FMT_EXTENTS:
110 case XFS_DINODE_FMT_BTREE:
111 buf->bs_rdev = 0;
112 buf->bs_blksize = mp->m_sb.sb_blocksize;
113 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
114 break;
115 }
Christoph Hellwigf2d67612010-06-24 11:52:50 +1000116 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700117 xfs_irele(ip);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000118
119 error = formatter(buffer, ubsize, ubused, buf);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000120 if (!error)
121 *stat = BULKSTAT_RV_DIDONE;
122
123 out_free:
124 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return error;
126}
127
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600128/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000129STATIC int
130xfs_bulkstat_one_fmt(
131 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600132 int ubsize,
133 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000134 const xfs_bstat_t *buffer)
135{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600136 if (ubsize < sizeof(*buffer))
Dave Chinner24513372014-06-25 14:58:08 +1000137 return -ENOMEM;
Michal Marekfaa63e92007-07-11 11:10:19 +1000138 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
Dave Chinner24513372014-06-25 14:58:08 +1000139 return -EFAULT;
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600140 if (ubused)
141 *ubused = sizeof(*buffer);
142 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000143}
144
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600145int
146xfs_bulkstat_one(
147 xfs_mount_t *mp, /* mount point for filesystem */
148 xfs_ino_t ino, /* inode number to get data for */
149 void __user *buffer, /* buffer to place output in */
150 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600151 int *ubused, /* bytes used by me */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600152 int *stat) /* BULKSTAT_RV_... */
153{
154 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000155 xfs_bulkstat_one_fmt, ubused, stat);
Nathan Scott8b56f082006-09-28 11:01:46 +1000156}
157
Jie Liu4b8fdfe2014-07-24 18:41:18 +1000158/*
159 * Loop over all clusters in a chunk for a given incore inode allocation btree
160 * record. Do a readahead if there are any allocated inodes in that cluster.
161 */
162STATIC void
163xfs_bulkstat_ichunk_ra(
164 struct xfs_mount *mp,
165 xfs_agnumber_t agno,
166 struct xfs_inobt_rec_incore *irec)
167{
168 xfs_agblock_t agbno;
169 struct blk_plug plug;
170 int blks_per_cluster;
171 int inodes_per_cluster;
172 int i; /* inode chunk index */
173
174 agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
175 blks_per_cluster = xfs_icluster_size_fsb(mp);
176 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
177
178 blk_start_plug(&plug);
179 for (i = 0; i < XFS_INODES_PER_CHUNK;
180 i += inodes_per_cluster, agbno += blks_per_cluster) {
181 if (xfs_inobt_maskn(i, inodes_per_cluster) & ~irec->ir_free) {
182 xfs_btree_reada_bufs(mp, agno, agbno, blks_per_cluster,
183 &xfs_inode_buf_ops);
184 }
185 }
186 blk_finish_plug(&plug);
187}
188
Jie Liuf3d1e582014-07-24 18:42:21 +1000189/*
190 * Lookup the inode chunk that the given inode lives in and then get the record
191 * if we found the chunk. If the inode was not the last in the chunk and there
192 * are some left allocated, update the data for the pointed-to record as well as
193 * return the count of grabbed inodes.
194 */
195STATIC int
196xfs_bulkstat_grab_ichunk(
197 struct xfs_btree_cur *cur, /* btree cursor */
198 xfs_agino_t agino, /* starting inode of chunk */
199 int *icount,/* return # of inodes grabbed */
200 struct xfs_inobt_rec_incore *irec) /* btree record */
201{
202 int idx; /* index into inode chunk */
203 int stat;
204 int error = 0;
205
206 /* Lookup the inode chunk that this inode lives in */
207 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
208 if (error)
209 return error;
210 if (!stat) {
211 *icount = 0;
212 return error;
213 }
214
215 /* Get the record, should always work */
216 error = xfs_inobt_get_rec(cur, irec, &stat);
217 if (error)
218 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100219 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1);
Jie Liuf3d1e582014-07-24 18:42:21 +1000220
221 /* Check if the record contains the inode in request */
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100222 if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) {
223 *icount = 0;
224 return 0;
225 }
Jie Liuf3d1e582014-07-24 18:42:21 +1000226
227 idx = agino - irec->ir_startino + 1;
228 if (idx < XFS_INODES_PER_CHUNK &&
229 (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
230 int i;
231
232 /* We got a right chunk with some left inodes allocated at it.
233 * Grab the chunk record. Mark all the uninteresting inodes
234 * free -- because they're before our start point.
235 */
236 for (i = 0; i < idx; i++) {
237 if (XFS_INOBT_MASK(i) & ~irec->ir_free)
238 irec->ir_freecount++;
239 }
240
241 irec->ir_free |= xfs_inobt_maskn(0, idx);
Brian Foster12d07142015-05-29 09:04:19 +1000242 *icount = irec->ir_count - irec->ir_freecount;
Jie Liuf3d1e582014-07-24 18:42:21 +1000243 }
244
245 return 0;
246}
247
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100248#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
249
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100250struct xfs_bulkstat_agichunk {
251 char __user **ac_ubuffer;/* pointer into user's buffer */
252 int ac_ubleft; /* bytes left in user's buffer */
253 int ac_ubelem; /* spaces used in user's buffer */
254};
255
Nathan Scott8b56f082006-09-28 11:01:46 +1000256/*
Jie Liu1e773c42014-08-04 11:22:31 +1000257 * Process inodes in chunk with a pointer to a formatter function
258 * that will iget the inode and fill in the appropriate structure.
259 */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100260static int
Jie Liu1e773c42014-08-04 11:22:31 +1000261xfs_bulkstat_ag_ichunk(
262 struct xfs_mount *mp,
263 xfs_agnumber_t agno,
264 struct xfs_inobt_rec_incore *irbp,
265 bulkstat_one_pf formatter,
266 size_t statstruct_size,
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100267 struct xfs_bulkstat_agichunk *acp,
Dave Chinner00275892014-11-07 08:33:52 +1100268 xfs_agino_t *last_agino)
Jie Liu1e773c42014-08-04 11:22:31 +1000269{
Jie Liu1e773c42014-08-04 11:22:31 +1000270 char __user **ubufp = acp->ac_ubuffer;
Dave Chinner2b831ac62014-11-07 08:30:58 +1100271 int chunkidx;
Jie Liu1e773c42014-08-04 11:22:31 +1000272 int error = 0;
Dave Chinner00275892014-11-07 08:33:52 +1100273 xfs_agino_t agino = irbp->ir_startino;
Jie Liu1e773c42014-08-04 11:22:31 +1000274
Dave Chinner2b831ac62014-11-07 08:30:58 +1100275 for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK;
276 chunkidx++, agino++) {
277 int fmterror;
Jie Liu1e773c42014-08-04 11:22:31 +1000278 int ubused;
Dave Chinner00275892014-11-07 08:33:52 +1100279
280 /* inode won't fit in buffer, we are done */
281 if (acp->ac_ubleft < statstruct_size)
282 break;
Jie Liu1e773c42014-08-04 11:22:31 +1000283
Jie Liu1e773c42014-08-04 11:22:31 +1000284 /* Skip if this inode is free */
Dave Chinner00275892014-11-07 08:33:52 +1100285 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
Jie Liu1e773c42014-08-04 11:22:31 +1000286 continue;
Jie Liu1e773c42014-08-04 11:22:31 +1000287
Jie Liu1e773c42014-08-04 11:22:31 +1000288 /* Get the inode and fill in a single buffer */
289 ubused = statstruct_size;
Dave Chinner00275892014-11-07 08:33:52 +1100290 error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino),
291 *ubufp, acp->ac_ubleft, &ubused, &fmterror);
292
Dave Chinner2b831ac62014-11-07 08:30:58 +1100293 if (fmterror == BULKSTAT_RV_GIVEUP ||
294 (error && error != -ENOENT && error != -EINVAL)) {
295 acp->ac_ubleft = 0;
Jie Liu1e773c42014-08-04 11:22:31 +1000296 ASSERT(error);
297 break;
298 }
Jie Liu1e773c42014-08-04 11:22:31 +1000299
Dave Chinner2b831ac62014-11-07 08:30:58 +1100300 /* be careful not to leak error if at end of chunk */
301 if (fmterror == BULKSTAT_RV_NOTHING || error) {
Dave Chinner2b831ac62014-11-07 08:30:58 +1100302 error = 0;
303 continue;
304 }
305
306 *ubufp += ubused;
307 acp->ac_ubleft -= ubused;
308 acp->ac_ubelem++;
Dave Chinner2b831ac62014-11-07 08:30:58 +1100309 }
Jie Liu1e773c42014-08-04 11:22:31 +1000310
Dave Chinner00275892014-11-07 08:33:52 +1100311 /*
312 * Post-update *last_agino. At this point, agino will always point one
313 * inode past the last inode we processed successfully. Hence we
314 * substract that inode when setting the *last_agino cursor so that we
315 * return the correct cookie to userspace. On the next bulkstat call,
316 * the inode under the lastino cookie will be skipped as we have already
317 * processed it here.
318 */
319 *last_agino = agino - 1;
320
Jie Liu1e773c42014-08-04 11:22:31 +1000321 return error;
322}
323
324/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * Return stat information in bulk (by-inode) for the filesystem.
326 */
327int /* error status */
328xfs_bulkstat(
329 xfs_mount_t *mp, /* mount point for filesystem */
330 xfs_ino_t *lastinop, /* last inode returned */
331 int *ubcountp, /* size of buffer/count returned */
332 bulkstat_one_pf formatter, /* func that'd fill a single buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 size_t statstruct_size, /* sizeof struct filling */
334 char __user *ubuffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000335 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_buf_t *agbp; /* agi header buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_agino_t agino; /* inode # in allocation group */
339 xfs_agnumber_t agno; /* allocation group number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
Nathan Scott262750932006-09-28 11:02:03 +1000341 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int nirbuf; /* size of irbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int ubcount; /* size of user's buffer */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100344 struct xfs_bulkstat_agichunk ac;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100345 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
348 * Get the last inode value, see if there's nothing to do.
349 */
Dave Chinner00275892014-11-07 08:33:52 +1100350 agno = XFS_INO_TO_AGNO(mp, *lastinop);
351 agino = XFS_INO_TO_AGINO(mp, *lastinop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (agno >= mp->m_sb.sb_agcount ||
Dave Chinner00275892014-11-07 08:33:52 +1100353 *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *done = 1;
355 *ubcountp = 0;
356 return 0;
357 }
Jie Liu296dfd72014-07-24 18:40:26 +1000358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ubcount = *ubcountp; /* statstruct's */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100360 ac.ac_ubuffer = &ubuffer;
361 ac.ac_ubleft = ubcount * statstruct_size; /* bytes */;
362 ac.ac_ubelem = 0;
363
364 *ubcountp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *done = 0;
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100366
Darrick J. Wong08b005f2017-03-06 11:58:20 -0800367 irbuf = kmem_zalloc_large(PAGE_SIZE * 4, KM_SLEEP);
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000368 if (!irbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000369 return -ENOMEM;
Darrick J. Wong08b005f2017-03-06 11:58:20 -0800370 nirbuf = (PAGE_SIZE * 4) / sizeof(*irbuf);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Loop over the allocation groups, starting from the last
374 * inode returned; 0 means start of the allocation group.
375 */
Dave Chinner6e57c5422014-11-07 08:31:13 +1100376 while (agno < mp->m_sb.sb_agcount) {
377 struct xfs_inobt_rec_incore *irbp = irbuf;
378 struct xfs_inobt_rec_incore *irbufend = irbuf + nirbuf;
379 bool end_of_ag = false;
380 int icount = 0;
381 int stat;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Jie Liud4c27342014-07-24 18:40:43 +1000384 if (error)
385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Allocate and initialize a btree cursor for ialloc btree.
388 */
Brian Foster57bd3db2014-04-24 16:00:50 +1000389 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
390 XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (agino > 0) {
392 /*
Jie Liuf3d1e582014-07-24 18:42:21 +1000393 * In the middle of an allocation group, we need to get
394 * the remainder of the chunk we're in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Jie Liuf3d1e582014-07-24 18:42:21 +1000396 struct xfs_inobt_rec_incore r;
397
398 error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
399 if (error)
Dave Chinnera6bbce52014-10-29 08:22:18 +1100400 goto del_cursor;
Jie Liuf3d1e582014-07-24 18:42:21 +1000401 if (icount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300402 irbp->ir_startino = r.ir_startino;
Brian Foster12d07142015-05-29 09:04:19 +1000403 irbp->ir_holemask = r.ir_holemask;
404 irbp->ir_count = r.ir_count;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300405 irbp->ir_freecount = r.ir_freecount;
406 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 irbp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Jie Liuf3d1e582014-07-24 18:42:21 +1000409 /* Increment to the next record */
Dave Chinnerafa947c2014-11-07 08:29:57 +1100410 error = xfs_btree_increment(cur, 0, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 } else {
Jie Liuf3d1e582014-07-24 18:42:21 +1000412 /* Start of ag. Lookup the first inode chunk */
Dave Chinnerafa947c2014-11-07 08:29:57 +1100413 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Dave Chinnerafa947c2014-11-07 08:29:57 +1100415 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100416 end_of_ag = true;
Dave Chinnera6bbce52014-10-29 08:22:18 +1100417 goto del_cursor;
Dave Chinnerafa947c2014-11-07 08:29:57 +1100418 }
Jie Liud4c27342014-07-24 18:40:43 +1000419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Loop through inode btree records in this ag,
422 * until we run out of inodes or space in the buffer.
423 */
424 while (irbp < irbufend && icount < ubcount) {
Jie Liud4c27342014-07-24 18:40:43 +1000425 struct xfs_inobt_rec_incore r;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300426
Dave Chinnerafa947c2014-11-07 08:29:57 +1100427 error = xfs_inobt_get_rec(cur, &r, &stat);
428 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100429 end_of_ag = true;
Dave Chinnera6bbce52014-10-29 08:22:18 +1100430 goto del_cursor;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300431 }
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000435 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Brian Foster12d07142015-05-29 09:04:19 +1000437 if (r.ir_freecount < r.ir_count) {
Jie Liu4b8fdfe2014-07-24 18:41:18 +1000438 xfs_bulkstat_ichunk_ra(mp, agno, &r);
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300439 irbp->ir_startino = r.ir_startino;
Brian Foster12d07142015-05-29 09:04:19 +1000440 irbp->ir_holemask = r.ir_holemask;
441 irbp->ir_count = r.ir_count;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300442 irbp->ir_freecount = r.ir_freecount;
443 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 irbp++;
Brian Foster12d07142015-05-29 09:04:19 +1000445 icount += r.ir_count - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Dave Chinnerafa947c2014-11-07 08:29:57 +1100447 error = xfs_btree_increment(cur, 0, &stat);
448 if (error || stat == 0) {
Dave Chinner6e57c5422014-11-07 08:31:13 +1100449 end_of_ag = true;
Jan Kara7a19dee2014-10-30 10:34:52 +1100450 goto del_cursor;
451 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100452 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Dave Chinnera6bbce52014-10-29 08:22:18 +1100454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
Dave Chinnera6bbce52014-10-29 08:22:18 +1100456 * Drop the btree buffers and the agi buffer as we can't hold any
457 * of the locks these represent when calling iget. If there is a
458 * pending error, then we are done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Dave Chinnera6bbce52014-10-29 08:22:18 +1100460del_cursor:
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700461 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_buf_relse(agbp);
Dave Chinnera6bbce52014-10-29 08:22:18 +1100463 if (error)
464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
Dave Chinner00275892014-11-07 08:33:52 +1100466 * Now format all the good inodes into the user's buffer. The
467 * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer
468 * for the next loop iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470 irbufend = irbp;
471 for (irbp = irbuf;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100472 irbp < irbufend && ac.ac_ubleft >= statstruct_size;
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100473 irbp++) {
Jie Liu1e773c42014-08-04 11:22:31 +1000474 error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100475 formatter, statstruct_size, &ac,
Dave Chinner00275892014-11-07 08:33:52 +1100476 &agino);
Jie Liu1e773c42014-08-04 11:22:31 +1000477 if (error)
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100480 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100482
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100483 /*
484 * If we've run out of space or had a formatting error, we
485 * are now done
486 */
487 if (ac.ac_ubleft < statstruct_size || error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
Dave Chinner6e57c5422014-11-07 08:31:13 +1100489
490 if (end_of_ag) {
491 agno++;
492 agino = 0;
Dave Chinner00275892014-11-07 08:33:52 +1100493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 /*
496 * Done, we're either out of filesystem or space to put the data.
497 */
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000498 kmem_free(irbuf);
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100499 *ubcountp = ac.ac_ubelem;
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100500
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100501 /*
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100502 * We found some inodes, so clear the error status and return them.
503 * The lastino pointer will point directly at the inode that triggered
504 * any error that occurred, so on the next call the error will be
505 * triggered again and propagated to userspace as there will be no
506 * formatted inodes in the buffer.
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100507 */
Dave Chinnerbf4a5af2014-11-07 08:30:30 +1100508 if (ac.ac_ubelem)
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100509 error = 0;
510
Dave Chinner00275892014-11-07 08:33:52 +1100511 /*
512 * If we ran out of filesystem, lastino will point off the end of
513 * the filesystem so the next call will return immediately.
514 */
515 *lastinop = XFS_AGINO_TO_INO(mp, agno, agino);
516 if (agno >= mp->m_sb.sb_agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Dave Chinnerfebe3cb2014-11-07 08:31:15 +1100519 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Michal Marekfaa63e92007-07-11 11:10:19 +1000522int
523xfs_inumbers_fmt(
524 void __user *ubuffer, /* buffer to write to */
Jie Liu549fa002014-07-24 12:11:47 +1000525 const struct xfs_inogrp *buffer, /* buffer to read from */
Michal Marekfaa63e92007-07-11 11:10:19 +1000526 long count, /* # of elements to read */
527 long *written) /* # of bytes written */
528{
529 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
530 return -EFAULT;
531 *written = count * sizeof(*buffer);
532 return 0;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * Return inode number table for the filesystem.
537 */
538int /* error status */
539xfs_inumbers(
Jie Liu549fa002014-07-24 12:11:47 +1000540 struct xfs_mount *mp,/* mount point for filesystem */
541 xfs_ino_t *lastino,/* last inode returned */
542 int *count,/* size of buffer/count returned */
543 void __user *ubuffer,/* buffer with inode descriptions */
544 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Jie Liu549fa002014-07-24 12:11:47 +1000546 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, *lastino);
547 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, *lastino);
548 struct xfs_btree_cur *cur = NULL;
Jie Liuc7cb51d2014-07-24 12:18:47 +1000549 struct xfs_buf *agbp = NULL;
Jie Liu549fa002014-07-24 12:11:47 +1000550 struct xfs_inogrp *buffer;
551 int bcount;
552 int left = *count;
553 int bufidx = 0;
554 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *count = 0;
Jie Liu549fa002014-07-24 12:11:47 +1000557 if (agno >= mp->m_sb.sb_agcount ||
558 *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
559 return error;
560
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700561 bcount = min(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Darrick J. Wongbf9216f2017-04-03 12:22:39 -0700562 buffer = kmem_zalloc(bcount * sizeof(*buffer), KM_SLEEP);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000563 do {
Jie Liu549fa002014-07-24 12:11:47 +1000564 struct xfs_inobt_rec_incore r;
565 int stat;
566
Jie Liuc7cb51d2014-07-24 12:18:47 +1000567 if (!agbp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000569 if (error)
570 break;
571
Brian Foster57bd3db2014-04-24 16:00:50 +1000572 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
573 XFS_BTNUM_INO);
Christoph Hellwig21875502009-08-31 20:58:21 -0300574 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
Jie Liu549fa002014-07-24 12:11:47 +1000575 &stat);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000576 if (error)
577 break;
578 if (!stat)
579 goto next_ag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000581
Jie Liu549fa002014-07-24 12:11:47 +1000582 error = xfs_inobt_get_rec(cur, &r, &stat);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000583 if (error)
584 break;
585 if (!stat)
586 goto next_ag;
587
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300588 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
589 buffer[bufidx].xi_startino =
590 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
Brian Foster12d07142015-05-29 09:04:19 +1000591 buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300592 buffer[bufidx].xi_allocmask = ~r.ir_free;
Jie Liuc7cb51d2014-07-24 12:18:47 +1000593 if (++bufidx == bcount) {
594 long written;
595
Jie Liu549fa002014-07-24 12:11:47 +1000596 error = formatter(ubuffer, buffer, bufidx, &written);
597 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Michal Marekfaa63e92007-07-11 11:10:19 +1000599 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *count += bufidx;
601 bufidx = 0;
602 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000603 if (!--left)
604 break;
605
606 error = xfs_btree_increment(cur, 0, &stat);
607 if (error)
608 break;
609 if (stat)
610 continue;
611
612next_ag:
613 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
614 cur = NULL;
615 xfs_buf_relse(agbp);
616 agbp = NULL;
617 agino = 0;
Eric Sandeena8b1ee82014-10-13 10:21:53 +1100618 agno++;
619 } while (agno < mp->m_sb.sb_agcount);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!error) {
622 if (bufidx) {
Jie Liuc7cb51d2014-07-24 12:18:47 +1000623 long written;
624
Jie Liu549fa002014-07-24 12:11:47 +1000625 error = formatter(ubuffer, buffer, bufidx, &written);
626 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *count += bufidx;
628 }
629 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
630 }
Jie Liuc7cb51d2014-07-24 12:18:47 +1000631
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000632 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700634 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (agbp)
636 xfs_buf_relse(agbp);
Jie Liuc7cb51d2014-07-24 12:18:47 +1000637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return error;
639}