eef307cf90a74aae92c9728dd9cb478c869ca218
[linux-2.6-microblaze.git] / fs / xfs / xfs_itable.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_btree.h"
15 #include "xfs_ialloc.h"
16 #include "xfs_ialloc_btree.h"
17 #include "xfs_itable.h"
18 #include "xfs_error.h"
19 #include "xfs_trace.h"
20 #include "xfs_icache.h"
21 #include "xfs_health.h"
22
23 /*
24  * Return stat information for one inode.
25  * Return 0 if ok, else errno.
26  */
27 int
28 xfs_bulkstat_one_int(
29         struct xfs_mount        *mp,            /* mount point for filesystem */
30         xfs_ino_t               ino,            /* inode to get data for */
31         void __user             *buffer,        /* buffer to place output in */
32         int                     ubsize,         /* size of buffer */
33         bulkstat_one_fmt_pf     formatter,      /* formatter, copy to user */
34         int                     *ubused,        /* bytes used by me */
35         int                     *stat)          /* BULKSTAT_RV_... */
36 {
37         struct xfs_icdinode     *dic;           /* dinode core info pointer */
38         struct xfs_inode        *ip;            /* incore inode pointer */
39         struct inode            *inode;
40         struct xfs_bstat        *buf;           /* return buffer */
41         int                     error = 0;      /* error value */
42
43         *stat = BULKSTAT_RV_NOTHING;
44
45         if (!buffer || xfs_internal_inum(mp, ino))
46                 return -EINVAL;
47
48         buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
49         if (!buf)
50                 return -ENOMEM;
51
52         error = xfs_iget(mp, NULL, ino,
53                          (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
54                          XFS_ILOCK_SHARED, &ip);
55         if (error)
56                 goto out_free;
57
58         ASSERT(ip != NULL);
59         ASSERT(ip->i_imap.im_blkno != 0);
60         inode = VFS_I(ip);
61
62         dic = &ip->i_d;
63
64         /* xfs_iget returns the following without needing
65          * further change.
66          */
67         buf->bs_projid_lo = dic->di_projid_lo;
68         buf->bs_projid_hi = dic->di_projid_hi;
69         buf->bs_ino = ino;
70         buf->bs_uid = dic->di_uid;
71         buf->bs_gid = dic->di_gid;
72         buf->bs_size = dic->di_size;
73
74         buf->bs_nlink = inode->i_nlink;
75         buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
76         buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
77         buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
78         buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
79         buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
80         buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
81         buf->bs_gen = inode->i_generation;
82         buf->bs_mode = inode->i_mode;
83
84         buf->bs_xflags = xfs_ip2xflags(ip);
85         buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
86         buf->bs_extents = dic->di_nextents;
87         memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
88         xfs_bulkstat_health(ip, buf);
89         buf->bs_dmevmask = dic->di_dmevmask;
90         buf->bs_dmstate = dic->di_dmstate;
91         buf->bs_aextents = dic->di_anextents;
92         buf->bs_forkoff = XFS_IFORK_BOFF(ip);
93
94         if (dic->di_version == 3) {
95                 if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
96                         buf->bs_cowextsize = dic->di_cowextsize <<
97                                         mp->m_sb.sb_blocklog;
98         }
99
100         switch (dic->di_format) {
101         case XFS_DINODE_FMT_DEV:
102                 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
103                 buf->bs_blksize = BLKDEV_IOSIZE;
104                 buf->bs_blocks = 0;
105                 break;
106         case XFS_DINODE_FMT_LOCAL:
107                 buf->bs_rdev = 0;
108                 buf->bs_blksize = mp->m_sb.sb_blocksize;
109                 buf->bs_blocks = 0;
110                 break;
111         case XFS_DINODE_FMT_EXTENTS:
112         case XFS_DINODE_FMT_BTREE:
113                 buf->bs_rdev = 0;
114                 buf->bs_blksize = mp->m_sb.sb_blocksize;
115                 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
116                 break;
117         }
118         xfs_iunlock(ip, XFS_ILOCK_SHARED);
119         xfs_irele(ip);
120
121         error = formatter(buffer, ubsize, ubused, buf);
122         if (!error)
123                 *stat = BULKSTAT_RV_DIDONE;
124
125  out_free:
126         kmem_free(buf);
127         return error;
128 }
129
130 /* Return 0 on success or positive error */
131 STATIC int
132 xfs_bulkstat_one_fmt(
133         void                    __user *ubuffer,
134         int                     ubsize,
135         int                     *ubused,
136         const xfs_bstat_t       *buffer)
137 {
138         if (ubsize < sizeof(*buffer))
139                 return -ENOMEM;
140         if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
141                 return -EFAULT;
142         if (ubused)
143                 *ubused = sizeof(*buffer);
144         return 0;
145 }
146
147 int
148 xfs_bulkstat_one(
149         xfs_mount_t     *mp,            /* mount point for filesystem */
150         xfs_ino_t       ino,            /* inode number to get data for */
151         void            __user *buffer, /* buffer to place output in */
152         int             ubsize,         /* size of buffer */
153         int             *ubused,        /* bytes used by me */
154         int             *stat)          /* BULKSTAT_RV_... */
155 {
156         return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
157                                     xfs_bulkstat_one_fmt, ubused, stat);
158 }
159
160 /*
161  * Loop over all clusters in a chunk for a given incore inode allocation btree
162  * record.  Do a readahead if there are any allocated inodes in that cluster.
163  */
164 STATIC void
165 xfs_bulkstat_ichunk_ra(
166         struct xfs_mount                *mp,
167         xfs_agnumber_t                  agno,
168         struct xfs_inobt_rec_incore     *irec)
169 {
170         struct xfs_ino_geometry         *igeo = M_IGEO(mp);
171         xfs_agblock_t                   agbno;
172         struct blk_plug                 plug;
173         int                             i;      /* inode chunk index */
174
175         agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
176
177         blk_start_plug(&plug);
178         for (i = 0;
179              i < XFS_INODES_PER_CHUNK;
180              i += igeo->inodes_per_cluster,
181                         agbno += igeo->blocks_per_cluster) {
182                 if (xfs_inobt_maskn(i, igeo->inodes_per_cluster) &
183                     ~irec->ir_free) {
184                         xfs_btree_reada_bufs(mp, agno, agbno,
185                                         igeo->blocks_per_cluster,
186                                         &xfs_inode_buf_ops);
187                 }
188         }
189         blk_finish_plug(&plug);
190 }
191
192 /*
193  * Lookup the inode chunk that the given inode lives in and then get the record
194  * if we found the chunk.  If the inode was not the last in the chunk and there
195  * are some left allocated, update the data for the pointed-to record as well as
196  * return the count of grabbed inodes.
197  */
198 STATIC int
199 xfs_bulkstat_grab_ichunk(
200         struct xfs_btree_cur            *cur,   /* btree cursor */
201         xfs_agino_t                     agino,  /* starting inode of chunk */
202         int                             *icount,/* return # of inodes grabbed */
203         struct xfs_inobt_rec_incore     *irec)  /* btree record */
204 {
205         int                             idx;    /* index into inode chunk */
206         int                             stat;
207         int                             error = 0;
208
209         /* Lookup the inode chunk that this inode lives in */
210         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
211         if (error)
212                 return error;
213         if (!stat) {
214                 *icount = 0;
215                 return error;
216         }
217
218         /* Get the record, should always work */
219         error = xfs_inobt_get_rec(cur, irec, &stat);
220         if (error)
221                 return error;
222         XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1);
223
224         /* Check if the record contains the inode in request */
225         if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) {
226                 *icount = 0;
227                 return 0;
228         }
229
230         idx = agino - irec->ir_startino + 1;
231         if (idx < XFS_INODES_PER_CHUNK &&
232             (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
233                 int     i;
234
235                 /* We got a right chunk with some left inodes allocated at it.
236                  * Grab the chunk record.  Mark all the uninteresting inodes
237                  * free -- because they're before our start point.
238                  */
239                 for (i = 0; i < idx; i++) {
240                         if (XFS_INOBT_MASK(i) & ~irec->ir_free)
241                                 irec->ir_freecount++;
242                 }
243
244                 irec->ir_free |= xfs_inobt_maskn(0, idx);
245                 *icount = irec->ir_count - irec->ir_freecount;
246         }
247
248         return 0;
249 }
250
251 #define XFS_BULKSTAT_UBLEFT(ubleft)     ((ubleft) >= statstruct_size)
252
253 struct xfs_bulkstat_agichunk {
254         char            __user **ac_ubuffer;/* pointer into user's buffer */
255         int             ac_ubleft;      /* bytes left in user's buffer */
256         int             ac_ubelem;      /* spaces used in user's buffer */
257 };
258
259 /*
260  * Process inodes in chunk with a pointer to a formatter function
261  * that will iget the inode and fill in the appropriate structure.
262  */
263 static int
264 xfs_bulkstat_ag_ichunk(
265         struct xfs_mount                *mp,
266         xfs_agnumber_t                  agno,
267         struct xfs_inobt_rec_incore     *irbp,
268         bulkstat_one_pf                 formatter,
269         size_t                          statstruct_size,
270         struct xfs_bulkstat_agichunk    *acp,
271         xfs_agino_t                     *last_agino)
272 {
273         char                            __user **ubufp = acp->ac_ubuffer;
274         int                             chunkidx;
275         int                             error = 0;
276         xfs_agino_t                     agino = irbp->ir_startino;
277
278         for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK;
279              chunkidx++, agino++) {
280                 int             fmterror;
281                 int             ubused;
282
283                 /* inode won't fit in buffer, we are done */
284                 if (acp->ac_ubleft < statstruct_size)
285                         break;
286
287                 /* Skip if this inode is free */
288                 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
289                         continue;
290
291                 /* Get the inode and fill in a single buffer */
292                 ubused = statstruct_size;
293                 error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino),
294                                   *ubufp, acp->ac_ubleft, &ubused, &fmterror);
295
296                 if (fmterror == BULKSTAT_RV_GIVEUP ||
297                     (error && error != -ENOENT && error != -EINVAL)) {
298                         acp->ac_ubleft = 0;
299                         ASSERT(error);
300                         break;
301                 }
302
303                 /* be careful not to leak error if at end of chunk */
304                 if (fmterror == BULKSTAT_RV_NOTHING || error) {
305                         error = 0;
306                         continue;
307                 }
308
309                 *ubufp += ubused;
310                 acp->ac_ubleft -= ubused;
311                 acp->ac_ubelem++;
312         }
313
314         /*
315          * Post-update *last_agino. At this point, agino will always point one
316          * inode past the last inode we processed successfully. Hence we
317          * substract that inode when setting the *last_agino cursor so that we
318          * return the correct cookie to userspace. On the next bulkstat call,
319          * the inode under the lastino cookie will be skipped as we have already
320          * processed it here.
321          */
322         *last_agino = agino - 1;
323
324         return error;
325 }
326
327 /*
328  * Return stat information in bulk (by-inode) for the filesystem.
329  */
330 int                                     /* error status */
331 xfs_bulkstat(
332         xfs_mount_t             *mp,    /* mount point for filesystem */
333         xfs_ino_t               *lastinop, /* last inode returned */
334         int                     *ubcountp, /* size of buffer/count returned */
335         bulkstat_one_pf         formatter, /* func that'd fill a single buf */
336         size_t                  statstruct_size, /* sizeof struct filling */
337         char                    __user *ubuffer, /* buffer with inode stats */
338         int                     *done)  /* 1 if there are more stats to get */
339 {
340         xfs_buf_t               *agbp;  /* agi header buffer */
341         xfs_agino_t             agino;  /* inode # in allocation group */
342         xfs_agnumber_t          agno;   /* allocation group number */
343         xfs_btree_cur_t         *cur;   /* btree cursor for ialloc btree */
344         xfs_inobt_rec_incore_t  *irbuf; /* start of irec buffer */
345         int                     nirbuf; /* size of irbuf */
346         int                     ubcount; /* size of user's buffer */
347         struct xfs_bulkstat_agichunk ac;
348         int                     error = 0;
349
350         /*
351          * Get the last inode value, see if there's nothing to do.
352          */
353         agno = XFS_INO_TO_AGNO(mp, *lastinop);
354         agino = XFS_INO_TO_AGINO(mp, *lastinop);
355         if (agno >= mp->m_sb.sb_agcount ||
356             *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) {
357                 *done = 1;
358                 *ubcountp = 0;
359                 return 0;
360         }
361
362         ubcount = *ubcountp; /* statstruct's */
363         ac.ac_ubuffer = &ubuffer;
364         ac.ac_ubleft = ubcount * statstruct_size; /* bytes */;
365         ac.ac_ubelem = 0;
366
367         *ubcountp = 0;
368         *done = 0;
369
370         irbuf = kmem_zalloc_large(PAGE_SIZE * 4, KM_SLEEP);
371         if (!irbuf)
372                 return -ENOMEM;
373         nirbuf = (PAGE_SIZE * 4) / sizeof(*irbuf);
374
375         /*
376          * Loop over the allocation groups, starting from the last
377          * inode returned; 0 means start of the allocation group.
378          */
379         while (agno < mp->m_sb.sb_agcount) {
380                 struct xfs_inobt_rec_incore     *irbp = irbuf;
381                 struct xfs_inobt_rec_incore     *irbufend = irbuf + nirbuf;
382                 bool                            end_of_ag = false;
383                 int                             icount = 0;
384                 int                             stat;
385
386                 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
387                 if (error)
388                         break;
389                 /*
390                  * Allocate and initialize a btree cursor for ialloc btree.
391                  */
392                 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
393                                             XFS_BTNUM_INO);
394                 if (agino > 0) {
395                         /*
396                          * In the middle of an allocation group, we need to get
397                          * the remainder of the chunk we're in.
398                          */
399                         struct xfs_inobt_rec_incore     r;
400
401                         error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
402                         if (error)
403                                 goto del_cursor;
404                         if (icount) {
405                                 irbp->ir_startino = r.ir_startino;
406                                 irbp->ir_holemask = r.ir_holemask;
407                                 irbp->ir_count = r.ir_count;
408                                 irbp->ir_freecount = r.ir_freecount;
409                                 irbp->ir_free = r.ir_free;
410                                 irbp++;
411                         }
412                         /* Increment to the next record */
413                         error = xfs_btree_increment(cur, 0, &stat);
414                 } else {
415                         /* Start of ag.  Lookup the first inode chunk */
416                         error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat);
417                 }
418                 if (error || stat == 0) {
419                         end_of_ag = true;
420                         goto del_cursor;
421                 }
422
423                 /*
424                  * Loop through inode btree records in this ag,
425                  * until we run out of inodes or space in the buffer.
426                  */
427                 while (irbp < irbufend && icount < ubcount) {
428                         struct xfs_inobt_rec_incore     r;
429
430                         error = xfs_inobt_get_rec(cur, &r, &stat);
431                         if (error || stat == 0) {
432                                 end_of_ag = true;
433                                 goto del_cursor;
434                         }
435
436                         /*
437                          * If this chunk has any allocated inodes, save it.
438                          * Also start read-ahead now for this chunk.
439                          */
440                         if (r.ir_freecount < r.ir_count) {
441                                 xfs_bulkstat_ichunk_ra(mp, agno, &r);
442                                 irbp->ir_startino = r.ir_startino;
443                                 irbp->ir_holemask = r.ir_holemask;
444                                 irbp->ir_count = r.ir_count;
445                                 irbp->ir_freecount = r.ir_freecount;
446                                 irbp->ir_free = r.ir_free;
447                                 irbp++;
448                                 icount += r.ir_count - r.ir_freecount;
449                         }
450                         error = xfs_btree_increment(cur, 0, &stat);
451                         if (error || stat == 0) {
452                                 end_of_ag = true;
453                                 goto del_cursor;
454                         }
455                         cond_resched();
456                 }
457
458                 /*
459                  * Drop the btree buffers and the agi buffer as we can't hold any
460                  * of the locks these represent when calling iget. If there is a
461                  * pending error, then we are done.
462                  */
463 del_cursor:
464                 xfs_btree_del_cursor(cur, error);
465                 xfs_buf_relse(agbp);
466                 if (error)
467                         break;
468                 /*
469                  * Now format all the good inodes into the user's buffer. The
470                  * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer
471                  * for the next loop iteration.
472                  */
473                 irbufend = irbp;
474                 for (irbp = irbuf;
475                      irbp < irbufend && ac.ac_ubleft >= statstruct_size;
476                      irbp++) {
477                         error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
478                                         formatter, statstruct_size, &ac,
479                                         &agino);
480                         if (error)
481                                 break;
482
483                         cond_resched();
484                 }
485
486                 /*
487                  * If we've run out of space or had a formatting error, we
488                  * are now done
489                  */
490                 if (ac.ac_ubleft < statstruct_size || error)
491                         break;
492
493                 if (end_of_ag) {
494                         agno++;
495                         agino = 0;
496                 }
497         }
498         /*
499          * Done, we're either out of filesystem or space to put the data.
500          */
501         kmem_free(irbuf);
502         *ubcountp = ac.ac_ubelem;
503
504         /*
505          * We found some inodes, so clear the error status and return them.
506          * The lastino pointer will point directly at the inode that triggered
507          * any error that occurred, so on the next call the error will be
508          * triggered again and propagated to userspace as there will be no
509          * formatted inodes in the buffer.
510          */
511         if (ac.ac_ubelem)
512                 error = 0;
513
514         /*
515          * If we ran out of filesystem, lastino will point off the end of
516          * the filesystem so the next call will return immediately.
517          */
518         *lastinop = XFS_AGINO_TO_INO(mp, agno, agino);
519         if (agno >= mp->m_sb.sb_agcount)
520                 *done = 1;
521
522         return error;
523 }
524
525 int
526 xfs_inumbers_fmt(
527         void                    __user *ubuffer, /* buffer to write to */
528         const struct xfs_inogrp *buffer,        /* buffer to read from */
529         long                    count,          /* # of elements to read */
530         long                    *written)       /* # of bytes written */
531 {
532         if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
533                 return -EFAULT;
534         *written = count * sizeof(*buffer);
535         return 0;
536 }
537
538 /*
539  * Return inode number table for the filesystem.
540  */
541 int                                     /* error status */
542 xfs_inumbers(
543         struct xfs_mount        *mp,/* mount point for filesystem */
544         xfs_ino_t               *lastino,/* last inode returned */
545         int                     *count,/* size of buffer/count returned */
546         void                    __user *ubuffer,/* buffer with inode descriptions */
547         inumbers_fmt_pf         formatter)
548 {
549         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, *lastino);
550         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, *lastino);
551         struct xfs_btree_cur    *cur = NULL;
552         struct xfs_buf          *agbp = NULL;
553         struct xfs_inogrp       *buffer;
554         int                     bcount;
555         int                     left = *count;
556         int                     bufidx = 0;
557         int                     error = 0;
558
559         *count = 0;
560         if (agno >= mp->m_sb.sb_agcount ||
561             *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
562                 return error;
563
564         bcount = min(left, (int)(PAGE_SIZE / sizeof(*buffer)));
565         buffer = kmem_zalloc(bcount * sizeof(*buffer), KM_SLEEP);
566         do {
567                 struct xfs_inobt_rec_incore     r;
568                 int                             stat;
569
570                 if (!agbp) {
571                         error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
572                         if (error)
573                                 break;
574
575                         cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
576                                                     XFS_BTNUM_INO);
577                         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
578                                                  &stat);
579                         if (error)
580                                 break;
581                         if (!stat)
582                                 goto next_ag;
583                 }
584
585                 error = xfs_inobt_get_rec(cur, &r, &stat);
586                 if (error)
587                         break;
588                 if (!stat)
589                         goto next_ag;
590
591                 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
592                 buffer[bufidx].xi_startino =
593                         XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
594                 buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount;
595                 buffer[bufidx].xi_allocmask = ~r.ir_free;
596                 if (++bufidx == bcount) {
597                         long    written;
598
599                         error = formatter(ubuffer, buffer, bufidx, &written);
600                         if (error)
601                                 break;
602                         ubuffer += written;
603                         *count += bufidx;
604                         bufidx = 0;
605                 }
606                 if (!--left)
607                         break;
608
609                 error = xfs_btree_increment(cur, 0, &stat);
610                 if (error)
611                         break;
612                 if (stat)
613                         continue;
614
615 next_ag:
616                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
617                 cur = NULL;
618                 xfs_buf_relse(agbp);
619                 agbp = NULL;
620                 agino = 0;
621                 agno++;
622         } while (agno < mp->m_sb.sb_agcount);
623
624         if (!error) {
625                 if (bufidx) {
626                         long    written;
627
628                         error = formatter(ubuffer, buffer, bufidx, &written);
629                         if (!error)
630                                 *count += bufidx;
631                 }
632                 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
633         }
634
635         kmem_free(buffer);
636         if (cur)
637                 xfs_btree_del_cursor(cur, error);
638         if (agbp)
639                 xfs_buf_relse(agbp);
640
641         return error;
642 }