Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 40 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 42 | int |
| 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 || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 48 | (xfs_sb_version_hasquota(&mp->m_sb) && |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 49 | (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino))); |
| 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | STATIC int |
| 53 | xfs_bulkstat_one_iget( |
| 54 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 55 | xfs_ino_t ino, /* inode number to get data for */ |
| 56 | xfs_daddr_t bno, /* starting bno of inode cluster */ |
| 57 | xfs_bstat_t *buf, /* return buffer */ |
| 58 | int *stat) /* BULKSTAT_RV_... */ |
| 59 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 60 | xfs_icdinode_t *dic; /* dinode core info pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | xfs_inode_t *ip; /* incore inode pointer */ |
| 62 | int error; |
| 63 | |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 64 | error = xfs_iget(mp, NULL, ino, |
| 65 | XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (error) { |
| 67 | *stat = BULKSTAT_RV_NOTHING; |
| 68 | return error; |
| 69 | } |
| 70 | |
| 71 | ASSERT(ip != NULL); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame^] | 72 | ASSERT(ip->i_imap.im_blkno != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | dic = &ip->i_d; |
| 75 | |
| 76 | /* xfs_iget returns the following without needing |
| 77 | * further change. |
| 78 | */ |
| 79 | buf->bs_nlink = dic->di_nlink; |
| 80 | buf->bs_projid = dic->di_projid; |
| 81 | buf->bs_ino = ino; |
| 82 | buf->bs_mode = dic->di_mode; |
| 83 | buf->bs_uid = dic->di_uid; |
| 84 | buf->bs_gid = dic->di_gid; |
| 85 | buf->bs_size = dic->di_size; |
Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 86 | vn_atime_to_bstime(VFS_I(ip), &buf->bs_atime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | buf->bs_mtime.tv_sec = dic->di_mtime.t_sec; |
| 88 | buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec; |
| 89 | buf->bs_ctime.tv_sec = dic->di_ctime.t_sec; |
| 90 | buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec; |
| 91 | buf->bs_xflags = xfs_ip2xflags(ip); |
| 92 | buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog; |
| 93 | buf->bs_extents = dic->di_nextents; |
| 94 | buf->bs_gen = dic->di_gen; |
| 95 | memset(buf->bs_pad, 0, sizeof(buf->bs_pad)); |
| 96 | buf->bs_dmevmask = dic->di_dmevmask; |
| 97 | buf->bs_dmstate = dic->di_dmstate; |
| 98 | buf->bs_aextents = dic->di_anextents; |
| 99 | |
| 100 | switch (dic->di_format) { |
| 101 | case XFS_DINODE_FMT_DEV: |
| 102 | buf->bs_rdev = ip->i_df.if_u2.if_rdev; |
| 103 | buf->bs_blksize = BLKDEV_IOSIZE; |
| 104 | buf->bs_blocks = 0; |
| 105 | break; |
| 106 | case XFS_DINODE_FMT_LOCAL: |
| 107 | case XFS_DINODE_FMT_UUID: |
| 108 | buf->bs_rdev = 0; |
| 109 | buf->bs_blksize = mp->m_sb.sb_blocksize; |
| 110 | buf->bs_blocks = 0; |
| 111 | break; |
| 112 | case XFS_DINODE_FMT_EXTENTS: |
| 113 | case XFS_DINODE_FMT_BTREE: |
| 114 | buf->bs_rdev = 0; |
| 115 | buf->bs_blksize = mp->m_sb.sb_blocksize; |
| 116 | buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks; |
| 117 | break; |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | xfs_iput(ip, XFS_ILOCK_SHARED); |
| 121 | return error; |
| 122 | } |
| 123 | |
David Chinner | 7b07339 | 2008-04-10 12:24:04 +1000 | [diff] [blame] | 124 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | xfs_bulkstat_one_dinode( |
| 126 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 127 | xfs_ino_t ino, /* inode number to get data for */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 128 | xfs_dinode_t *dic, /* dinode inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | xfs_bstat_t *buf) /* return buffer */ |
| 130 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* |
| 132 | * The inode format changed when we moved the link count and |
| 133 | * made it 32 bits long. If this is an old format inode, |
| 134 | * convert it in memory to look like a new one. If it gets |
| 135 | * flushed to disk we will convert back before flushing or |
| 136 | * logging it. We zero out the new projid field and the old link |
| 137 | * count field. We'll handle clearing the pad field (the remains |
| 138 | * of the old uuid field) when we actually convert the inode to |
| 139 | * the new format. We don't change the version number so that we |
| 140 | * can distinguish this from a real new format inode. |
| 141 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 142 | if (dic->di_version == 1) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 143 | buf->bs_nlink = be16_to_cpu(dic->di_onlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | buf->bs_projid = 0; |
| 145 | } else { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 146 | buf->bs_nlink = be32_to_cpu(dic->di_nlink); |
| 147 | buf->bs_projid = be16_to_cpu(dic->di_projid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | buf->bs_ino = ino; |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 151 | buf->bs_mode = be16_to_cpu(dic->di_mode); |
| 152 | buf->bs_uid = be32_to_cpu(dic->di_uid); |
| 153 | buf->bs_gid = be32_to_cpu(dic->di_gid); |
| 154 | buf->bs_size = be64_to_cpu(dic->di_size); |
| 155 | buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec); |
| 156 | buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec); |
| 157 | buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec); |
| 158 | buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec); |
| 159 | buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec); |
| 160 | buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 161 | buf->bs_xflags = xfs_dic2xflags(dic); |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 162 | buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog; |
| 163 | buf->bs_extents = be32_to_cpu(dic->di_nextents); |
| 164 | buf->bs_gen = be32_to_cpu(dic->di_gen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | memset(buf->bs_pad, 0, sizeof(buf->bs_pad)); |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 166 | buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask); |
| 167 | buf->bs_dmstate = be16_to_cpu(dic->di_dmstate); |
| 168 | buf->bs_aextents = be16_to_cpu(dic->di_anextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 170 | switch (dic->di_format) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | case XFS_DINODE_FMT_DEV: |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 172 | buf->bs_rdev = xfs_dinode_get_rdev(dic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | buf->bs_blksize = BLKDEV_IOSIZE; |
| 174 | buf->bs_blocks = 0; |
| 175 | break; |
| 176 | case XFS_DINODE_FMT_LOCAL: |
| 177 | case XFS_DINODE_FMT_UUID: |
| 178 | buf->bs_rdev = 0; |
| 179 | buf->bs_blksize = mp->m_sb.sb_blocksize; |
| 180 | buf->bs_blocks = 0; |
| 181 | break; |
| 182 | case XFS_DINODE_FMT_EXTENTS: |
| 183 | case XFS_DINODE_FMT_BTREE: |
| 184 | buf->bs_rdev = 0; |
| 185 | buf->bs_blksize = mp->m_sb.sb_blocksize; |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 186 | buf->bs_blocks = be64_to_cpu(dic->di_nblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | break; |
| 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 191 | STATIC int |
| 192 | xfs_bulkstat_one_fmt( |
| 193 | void __user *ubuffer, |
| 194 | const xfs_bstat_t *buffer) |
| 195 | { |
| 196 | if (copy_to_user(ubuffer, buffer, sizeof(*buffer))) |
| 197 | return -EFAULT; |
| 198 | return sizeof(*buffer); |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* |
| 202 | * Return stat information for one inode. |
| 203 | * Return 0 if ok, else errno. |
| 204 | */ |
| 205 | int /* error status */ |
| 206 | xfs_bulkstat_one( |
| 207 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 208 | xfs_ino_t ino, /* inode number to get data for */ |
| 209 | void __user *buffer, /* buffer to place output in */ |
| 210 | int ubsize, /* size of buffer */ |
| 211 | void *private_data, /* my private data */ |
| 212 | xfs_daddr_t bno, /* starting bno of inode cluster */ |
| 213 | int *ubused, /* bytes used by me */ |
| 214 | void *dibuff, /* on-disk inode buffer */ |
| 215 | int *stat) /* BULKSTAT_RV_... */ |
| 216 | { |
| 217 | xfs_bstat_t *buf; /* return buffer */ |
| 218 | int error = 0; /* error value */ |
| 219 | xfs_dinode_t *dip; /* dinode inode pointer */ |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 220 | bulkstat_one_fmt_pf formatter = private_data ? : xfs_bulkstat_one_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | dip = (xfs_dinode_t *)dibuff; |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 223 | *stat = BULKSTAT_RV_NOTHING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 225 | if (!buffer || xfs_internal_inum(mp, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return XFS_ERROR(EINVAL); |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 227 | if (ubsize < sizeof(*buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return XFS_ERROR(ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | buf = kmem_alloc(sizeof(*buf), KM_SLEEP); |
| 231 | |
| 232 | if (dip == NULL) { |
| 233 | /* We're not being passed a pointer to a dinode. This happens |
| 234 | * if BULKSTAT_FG_IGET is selected. Do the iget. |
| 235 | */ |
| 236 | error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat); |
| 237 | if (error) |
| 238 | goto out_free; |
| 239 | } else { |
| 240 | xfs_bulkstat_one_dinode(mp, ino, dip, buf); |
| 241 | } |
| 242 | |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 243 | error = formatter(buffer, buf); |
| 244 | if (error < 0) { |
Vlad Apostolov | 6f1f216 | 2006-09-28 11:06:15 +1000 | [diff] [blame] | 245 | error = EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | goto out_free; |
| 247 | } |
| 248 | |
| 249 | *stat = BULKSTAT_RV_DIDONE; |
| 250 | if (ubused) |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 251 | *ubused = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | out_free: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 254 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return error; |
| 256 | } |
| 257 | |
| 258 | /* |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 259 | * Test to see whether we can use the ondisk inode directly, based |
| 260 | * on the given bulkstat flags, filling in dipp accordingly. |
| 261 | * Returns zero if the inode is dodgey. |
| 262 | */ |
| 263 | STATIC int |
| 264 | xfs_bulkstat_use_dinode( |
| 265 | xfs_mount_t *mp, |
| 266 | int flags, |
| 267 | xfs_buf_t *bp, |
| 268 | int clustidx, |
| 269 | xfs_dinode_t **dipp) |
| 270 | { |
| 271 | xfs_dinode_t *dip; |
| 272 | unsigned int aformat; |
| 273 | |
| 274 | *dipp = NULL; |
| 275 | if (!bp || (flags & BULKSTAT_FG_IGET)) |
| 276 | return 1; |
| 277 | dip = (xfs_dinode_t *) |
| 278 | xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog); |
Vlad Apostolov | 859d718 | 2007-10-11 17:44:18 +1000 | [diff] [blame] | 279 | /* |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 280 | * Check the buffer containing the on-disk inode for di_mode == 0. |
Vlad Apostolov | 859d718 | 2007-10-11 17:44:18 +1000 | [diff] [blame] | 281 | * This is to prevent xfs_bulkstat from picking up just reclaimed |
| 282 | * inodes that have their in-core state initialized but not flushed |
| 283 | * to disk yet. This is a temporary hack that would require a proper |
| 284 | * fix in the future. |
| 285 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 286 | if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC || |
| 287 | !XFS_DINODE_GOOD_VERSION(dip->di_version) || |
| 288 | !dip->di_mode) |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 289 | return 0; |
| 290 | if (flags & BULKSTAT_FG_QUICK) { |
| 291 | *dipp = dip; |
| 292 | return 1; |
| 293 | } |
| 294 | /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 295 | aformat = dip->di_aformat; |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 296 | if ((XFS_DFORK_Q(dip) == 0) || |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 297 | (aformat == XFS_DINODE_FMT_LOCAL) || |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 298 | (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) { |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 299 | *dipp = dip; |
| 300 | return 1; |
| 301 | } |
| 302 | return 1; |
| 303 | } |
| 304 | |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 305 | #define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size) |
| 306 | |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 307 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * Return stat information in bulk (by-inode) for the filesystem. |
| 309 | */ |
| 310 | int /* error status */ |
| 311 | xfs_bulkstat( |
| 312 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 313 | xfs_ino_t *lastinop, /* last inode returned */ |
| 314 | int *ubcountp, /* size of buffer/count returned */ |
| 315 | bulkstat_one_pf formatter, /* func that'd fill a single buf */ |
| 316 | void *private_data,/* private data for formatter */ |
| 317 | size_t statstruct_size, /* sizeof struct filling */ |
| 318 | char __user *ubuffer, /* buffer with inode stats */ |
| 319 | int flags, /* defined in xfs_itable.h */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 320 | int *done) /* 1 if there are more stats to get */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | xfs_agblock_t agbno=0;/* allocation group block number */ |
| 323 | xfs_buf_t *agbp; /* agi header buffer */ |
| 324 | xfs_agi_t *agi; /* agi header data */ |
| 325 | xfs_agino_t agino; /* inode # in allocation group */ |
| 326 | xfs_agnumber_t agno; /* allocation group number */ |
| 327 | xfs_daddr_t bno; /* inode cluster start daddr */ |
| 328 | int chunkidx; /* current index into inode chunk */ |
| 329 | int clustidx; /* current index into inode cluster */ |
| 330 | xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */ |
| 331 | int end_of_ag; /* set if we've seen the ag end */ |
| 332 | int error; /* error code */ |
| 333 | int fmterror;/* bulkstat formatter result */ |
| 334 | __int32_t gcnt; /* current btree rec's count */ |
| 335 | xfs_inofree_t gfree; /* current btree rec's free mask */ |
| 336 | xfs_agino_t gino; /* current btree rec's start inode */ |
| 337 | int i; /* loop index */ |
| 338 | int icount; /* count of inodes good in irbuf */ |
Nathan Scott | 215101c | 2006-09-28 11:04:43 +1000 | [diff] [blame] | 339 | size_t irbsize; /* size of irec buffer in bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | xfs_ino_t ino; /* inode number (filesystem) */ |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 341 | xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */ |
| 342 | xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */ |
| 343 | xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */ |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 344 | xfs_ino_t lastino; /* last inode number returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | int nbcluster; /* # of blocks in a cluster */ |
| 346 | int nicluster; /* # of inodes in a cluster */ |
| 347 | int nimask; /* mask for inode clusters */ |
| 348 | int nirbuf; /* size of irbuf */ |
| 349 | int rval; /* return value error code */ |
| 350 | int tmp; /* result value from btree calls */ |
| 351 | int ubcount; /* size of user's buffer */ |
| 352 | int ubleft; /* bytes left in user's buffer */ |
| 353 | char __user *ubufp; /* pointer into user's buffer */ |
| 354 | int ubelem; /* spaces used in user's buffer */ |
| 355 | int ubused; /* bytes used by formatter */ |
| 356 | xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */ |
| 357 | xfs_dinode_t *dip; /* ptr into bp for specific inode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * Get the last inode value, see if there's nothing to do. |
| 361 | */ |
| 362 | ino = (xfs_ino_t)*lastinop; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 363 | lastino = ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | dip = NULL; |
| 365 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 366 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 367 | if (agno >= mp->m_sb.sb_agcount || |
| 368 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 369 | *done = 1; |
| 370 | *ubcountp = 0; |
| 371 | return 0; |
| 372 | } |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 373 | if (!ubcountp || *ubcountp <= 0) { |
| 374 | return EINVAL; |
| 375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | ubcount = *ubcountp; /* statstruct's */ |
| 377 | ubleft = ubcount * statstruct_size; /* bytes */ |
| 378 | *ubcountp = ubelem = 0; |
| 379 | *done = 0; |
| 380 | fmterror = 0; |
| 381 | ubufp = ubuffer; |
| 382 | nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ? |
| 383 | mp->m_sb.sb_inopblock : |
| 384 | (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog); |
| 385 | nimask = ~(nicluster - 1); |
| 386 | nbcluster = nicluster >> mp->m_sb.sb_inopblog; |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 387 | irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4, |
Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 388 | KM_SLEEP | KM_MAYFAIL | KM_LARGE); |
Nathan Scott | bb3c7d2 | 2006-09-28 11:02:09 +1000 | [diff] [blame] | 389 | nirbuf = irbsize / sizeof(*irbuf); |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* |
| 392 | * Loop over the allocation groups, starting from the last |
| 393 | * inode returned; 0 means start of the allocation group. |
| 394 | */ |
| 395 | rval = 0; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 396 | while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) { |
| 397 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | bp = NULL; |
| 399 | down_read(&mp->m_peraglock); |
| 400 | error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); |
| 401 | up_read(&mp->m_peraglock); |
| 402 | if (error) { |
| 403 | /* |
| 404 | * Skip this allocation group and go to the next one. |
| 405 | */ |
| 406 | agno++; |
| 407 | agino = 0; |
| 408 | continue; |
| 409 | } |
| 410 | agi = XFS_BUF_TO_AGI(agbp); |
| 411 | /* |
| 412 | * Allocate and initialize a btree cursor for ialloc btree. |
| 413 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 414 | cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | irbp = irbuf; |
| 416 | irbufend = irbuf + nirbuf; |
| 417 | end_of_ag = 0; |
| 418 | /* |
| 419 | * If we're returning in the middle of an allocation group, |
| 420 | * we need to get the remainder of the chunk we're in. |
| 421 | */ |
| 422 | if (agino > 0) { |
| 423 | /* |
| 424 | * Lookup the inode chunk that this inode lives in. |
| 425 | */ |
| 426 | error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp); |
| 427 | if (!error && /* no I/O error */ |
| 428 | tmp && /* lookup succeeded */ |
| 429 | /* got the record, should always work */ |
| 430 | !(error = xfs_inobt_get_rec(cur, &gino, &gcnt, |
| 431 | &gfree, &i)) && |
| 432 | i == 1 && |
| 433 | /* this is the right chunk */ |
| 434 | agino < gino + XFS_INODES_PER_CHUNK && |
| 435 | /* lastino was not last in chunk */ |
| 436 | (chunkidx = agino - gino + 1) < |
| 437 | XFS_INODES_PER_CHUNK && |
| 438 | /* there are some left allocated */ |
| 439 | XFS_INOBT_MASKN(chunkidx, |
| 440 | XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) { |
| 441 | /* |
| 442 | * Grab the chunk record. Mark all the |
| 443 | * uninteresting inodes (because they're |
| 444 | * before our start point) free. |
| 445 | */ |
| 446 | for (i = 0; i < chunkidx; i++) { |
| 447 | if (XFS_INOBT_MASK(i) & ~gfree) |
| 448 | gcnt++; |
| 449 | } |
| 450 | gfree |= XFS_INOBT_MASKN(0, chunkidx); |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 451 | irbp->ir_startino = gino; |
| 452 | irbp->ir_freecount = gcnt; |
| 453 | irbp->ir_free = gfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | irbp++; |
| 455 | agino = gino + XFS_INODES_PER_CHUNK; |
| 456 | icount = XFS_INODES_PER_CHUNK - gcnt; |
| 457 | } else { |
| 458 | /* |
| 459 | * If any of those tests failed, bump the |
| 460 | * inode number (just in case). |
| 461 | */ |
| 462 | agino++; |
| 463 | icount = 0; |
| 464 | } |
| 465 | /* |
| 466 | * In any case, increment to the next record. |
| 467 | */ |
| 468 | if (!error) |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 469 | error = xfs_btree_increment(cur, 0, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } else { |
| 471 | /* |
| 472 | * Start of ag. Lookup the first inode chunk. |
| 473 | */ |
| 474 | error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp); |
| 475 | icount = 0; |
| 476 | } |
| 477 | /* |
| 478 | * Loop through inode btree records in this ag, |
| 479 | * until we run out of inodes or space in the buffer. |
| 480 | */ |
| 481 | while (irbp < irbufend && icount < ubcount) { |
| 482 | /* |
| 483 | * Loop as long as we're unable to read the |
| 484 | * inode btree. |
| 485 | */ |
| 486 | while (error) { |
| 487 | agino += XFS_INODES_PER_CHUNK; |
| 488 | if (XFS_AGINO_TO_AGBNO(mp, agino) >= |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 489 | be32_to_cpu(agi->agi_length)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | break; |
| 491 | error = xfs_inobt_lookup_ge(cur, agino, 0, 0, |
| 492 | &tmp); |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 493 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | /* |
| 496 | * If ran off the end of the ag either with an error, |
| 497 | * or the normal way, set end and stop collecting. |
| 498 | */ |
| 499 | if (error || |
| 500 | (error = xfs_inobt_get_rec(cur, &gino, &gcnt, |
| 501 | &gfree, &i)) || |
| 502 | i == 0) { |
| 503 | end_of_ag = 1; |
| 504 | break; |
| 505 | } |
| 506 | /* |
| 507 | * If this chunk has any allocated inodes, save it. |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 508 | * Also start read-ahead now for this chunk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | */ |
| 510 | if (gcnt < XFS_INODES_PER_CHUNK) { |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 511 | /* |
| 512 | * Loop over all clusters in the next chunk. |
| 513 | * Do a readahead if there are any allocated |
| 514 | * inodes in that cluster. |
| 515 | */ |
| 516 | for (agbno = XFS_AGINO_TO_AGBNO(mp, gino), |
| 517 | chunkidx = 0; |
| 518 | chunkidx < XFS_INODES_PER_CHUNK; |
| 519 | chunkidx += nicluster, |
| 520 | agbno += nbcluster) { |
| 521 | if (XFS_INOBT_MASKN(chunkidx, |
| 522 | nicluster) & ~gfree) |
| 523 | xfs_btree_reada_bufs(mp, agno, |
| 524 | agbno, nbcluster); |
| 525 | } |
| 526 | irbp->ir_startino = gino; |
| 527 | irbp->ir_freecount = gcnt; |
| 528 | irbp->ir_free = gfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | irbp++; |
| 530 | icount += XFS_INODES_PER_CHUNK - gcnt; |
| 531 | } |
| 532 | /* |
| 533 | * Set agino to after this chunk and bump the cursor. |
| 534 | */ |
| 535 | agino = gino + XFS_INODES_PER_CHUNK; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 536 | error = xfs_btree_increment(cur, 0, &tmp); |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 537 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | /* |
| 540 | * Drop the btree buffers and the agi buffer. |
| 541 | * We can't hold any of the locks these represent |
| 542 | * when calling iget. |
| 543 | */ |
| 544 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 545 | xfs_buf_relse(agbp); |
| 546 | /* |
| 547 | * Now format all the good inodes into the user's buffer. |
| 548 | */ |
| 549 | irbufend = irbp; |
| 550 | for (irbp = irbuf; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 551 | irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | * Now process this chunk of inodes. |
| 554 | */ |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 555 | for (agino = irbp->ir_startino, chunkidx = clustidx = 0; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 556 | XFS_BULKSTAT_UBLEFT(ubleft) && |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 557 | irbp->ir_freecount < XFS_INODES_PER_CHUNK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | chunkidx++, clustidx++, agino++) { |
| 559 | ASSERT(chunkidx < XFS_INODES_PER_CHUNK); |
| 560 | /* |
| 561 | * Recompute agbno if this is the |
| 562 | * first inode of the cluster. |
| 563 | * |
| 564 | * Careful with clustidx. There can be |
| 565 | * multple clusters per chunk, a single |
| 566 | * cluster per chunk or a cluster that has |
| 567 | * inodes represented from several different |
| 568 | * chunks (if blocksize is large). |
| 569 | * |
| 570 | * Because of this, the starting clustidx is |
| 571 | * initialized to zero in this loop but must |
| 572 | * later be reset after reading in the cluster |
| 573 | * buffer. |
| 574 | */ |
| 575 | if ((chunkidx & (nicluster - 1)) == 0) { |
| 576 | agbno = XFS_AGINO_TO_AGBNO(mp, |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 577 | irbp->ir_startino) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | ((chunkidx & nimask) >> |
| 579 | mp->m_sb.sb_inopblog); |
| 580 | |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 581 | if (flags & (BULKSTAT_FG_QUICK | |
| 582 | BULKSTAT_FG_INLINE)) { |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 583 | int offset; |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | ino = XFS_AGINO_TO_INO(mp, agno, |
| 586 | agino); |
| 587 | bno = XFS_AGB_TO_DADDR(mp, agno, |
| 588 | agbno); |
| 589 | |
| 590 | /* |
| 591 | * Get the inode cluster buffer |
| 592 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | if (bp) |
| 594 | xfs_buf_relse(bp); |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 595 | |
| 596 | error = xfs_inotobp(mp, NULL, ino, &dip, |
| 597 | &bp, &offset, |
| 598 | XFS_IMAP_BULKSTAT); |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (!error) |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 601 | clustidx = offset / mp->m_sb.sb_inodesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (XFS_TEST_ERROR(error != 0, |
| 603 | mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK, |
| 604 | XFS_RANDOM_BULKSTAT_READ_CHUNK)) { |
| 605 | bp = NULL; |
Nathan Scott | b12dd34 | 2006-03-17 17:26:04 +1100 | [diff] [blame] | 606 | ubleft = 0; |
| 607 | rval = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | } |
| 611 | } |
Lachlan McIlroy | c2cba57 | 2007-10-12 11:12:20 +1000 | [diff] [blame] | 612 | ino = XFS_AGINO_TO_INO(mp, agno, agino); |
| 613 | bno = XFS_AGB_TO_DADDR(mp, agno, agbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | /* |
| 615 | * Skip if this inode is free. |
| 616 | */ |
Lachlan McIlroy | c2cba57 | 2007-10-12 11:12:20 +1000 | [diff] [blame] | 617 | if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) { |
| 618 | lastino = ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | continue; |
Lachlan McIlroy | c2cba57 | 2007-10-12 11:12:20 +1000 | [diff] [blame] | 620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | /* |
| 622 | * Count used inodes as free so we can tell |
| 623 | * when the chunk is used up. |
| 624 | */ |
Nathan Scott | 26275093 | 2006-09-28 11:02:03 +1000 | [diff] [blame] | 625 | irbp->ir_freecount++; |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 626 | if (!xfs_bulkstat_use_dinode(mp, flags, bp, |
Lachlan McIlroy | c2cba57 | 2007-10-12 11:12:20 +1000 | [diff] [blame] | 627 | clustidx, &dip)) { |
| 628 | lastino = ino; |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 629 | continue; |
Lachlan McIlroy | c2cba57 | 2007-10-12 11:12:20 +1000 | [diff] [blame] | 630 | } |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 631 | /* |
| 632 | * If we need to do an iget, cannot hold bp. |
| 633 | * Drop it, until starting the next cluster. |
| 634 | */ |
| 635 | if ((flags & BULKSTAT_FG_INLINE) && !dip) { |
| 636 | if (bp) |
| 637 | xfs_buf_relse(bp); |
| 638 | bp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Get the inode and fill in a single buffer. |
| 643 | * BULKSTAT_FG_QUICK uses dip to fill it in. |
| 644 | * BULKSTAT_FG_IGET uses igets. |
Nathan Scott | 8b56f08 | 2006-09-28 11:01:46 +1000 | [diff] [blame] | 645 | * BULKSTAT_FG_INLINE uses dip if we have an |
| 646 | * inline attr fork, else igets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | * See: xfs_bulkstat_one & xfs_dm_bulkstat_one. |
| 648 | * This is also used to count inodes/blks, etc |
| 649 | * in xfs_qm_quotacheck. |
| 650 | */ |
| 651 | ubused = statstruct_size; |
| 652 | error = formatter(mp, ino, ubufp, |
| 653 | ubleft, private_data, |
| 654 | bno, &ubused, dip, &fmterror); |
| 655 | if (fmterror == BULKSTAT_RV_NOTHING) { |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 656 | if (error && error != ENOENT && |
| 657 | error != EINVAL) { |
Vlad Apostolov | e132f54 | 2006-09-28 11:04:31 +1000 | [diff] [blame] | 658 | ubleft = 0; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 659 | rval = error; |
| 660 | break; |
| 661 | } |
| 662 | lastino = ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | continue; |
| 664 | } |
| 665 | if (fmterror == BULKSTAT_RV_GIVEUP) { |
| 666 | ubleft = 0; |
| 667 | ASSERT(error); |
| 668 | rval = error; |
| 669 | break; |
| 670 | } |
| 671 | if (ubufp) |
| 672 | ubufp += ubused; |
| 673 | ubleft -= ubused; |
| 674 | ubelem++; |
| 675 | lastino = ino; |
| 676 | } |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 677 | |
| 678 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (bp) |
| 682 | xfs_buf_relse(bp); |
| 683 | |
| 684 | /* |
| 685 | * Set up for the next loop iteration. |
| 686 | */ |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 687 | if (XFS_BULKSTAT_UBLEFT(ubleft)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | if (end_of_ag) { |
| 689 | agno++; |
| 690 | agino = 0; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 691 | } else |
| 692 | agino = XFS_INO_TO_AGINO(mp, lastino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } else |
| 694 | break; |
| 695 | } |
| 696 | /* |
| 697 | * Done, we're either out of filesystem or space to put the data. |
| 698 | */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 699 | kmem_free(irbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | *ubcountp = ubelem; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 701 | /* |
| 702 | * Found some inodes, return them now and return the error next time. |
| 703 | */ |
| 704 | if (ubelem) |
| 705 | rval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | if (agno >= mp->m_sb.sb_agcount) { |
| 707 | /* |
| 708 | * If we ran out of filesystem, mark lastino as off |
| 709 | * the end of the filesystem, so the next call |
| 710 | * will return immediately. |
| 711 | */ |
| 712 | *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0); |
| 713 | *done = 1; |
| 714 | } else |
| 715 | *lastinop = (xfs_ino_t)lastino; |
| 716 | |
| 717 | return rval; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Return stat information in bulk (by-inode) for the filesystem. |
| 722 | * Special case for non-sequential one inode bulkstat. |
| 723 | */ |
| 724 | int /* error status */ |
| 725 | xfs_bulkstat_single( |
| 726 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 727 | xfs_ino_t *lastinop, /* inode to return */ |
| 728 | char __user *buffer, /* buffer with inode stats */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 729 | int *done) /* 1 if there are more stats to get */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
| 731 | int count; /* count value for bulkstat call */ |
| 732 | int error; /* return value */ |
| 733 | xfs_ino_t ino; /* filesystem inode number */ |
| 734 | int res; /* result from bs1 */ |
| 735 | |
| 736 | /* |
| 737 | * note that requesting valid inode numbers which are not allocated |
| 738 | * to inodes will most likely cause xfs_itobp to generate warning |
| 739 | * messages about bad magic numbers. This is ok. The fact that |
| 740 | * the inode isn't actually an inode is handled by the |
| 741 | * error check below. Done this way to make the usual case faster |
| 742 | * at the expense of the error case. |
| 743 | */ |
| 744 | |
| 745 | ino = (xfs_ino_t)*lastinop; |
| 746 | error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), |
| 747 | NULL, 0, NULL, NULL, &res); |
| 748 | if (error) { |
| 749 | /* |
| 750 | * Special case way failed, do it the "long" way |
| 751 | * to see if that works. |
| 752 | */ |
| 753 | (*lastinop)--; |
| 754 | count = 1; |
| 755 | if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one, |
| 756 | NULL, sizeof(xfs_bstat_t), buffer, |
| 757 | BULKSTAT_FG_IGET, done)) |
| 758 | return error; |
| 759 | if (count == 0 || (xfs_ino_t)*lastinop != ino) |
| 760 | return error == EFSCORRUPTED ? |
| 761 | XFS_ERROR(EINVAL) : error; |
| 762 | else |
| 763 | return 0; |
| 764 | } |
| 765 | *done = 0; |
| 766 | return 0; |
| 767 | } |
| 768 | |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 769 | int |
| 770 | xfs_inumbers_fmt( |
| 771 | void __user *ubuffer, /* buffer to write to */ |
| 772 | const xfs_inogrp_t *buffer, /* buffer to read from */ |
| 773 | long count, /* # of elements to read */ |
| 774 | long *written) /* # of bytes written */ |
| 775 | { |
| 776 | if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer))) |
| 777 | return -EFAULT; |
| 778 | *written = count * sizeof(*buffer); |
| 779 | return 0; |
| 780 | } |
| 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | /* |
| 783 | * Return inode number table for the filesystem. |
| 784 | */ |
| 785 | int /* error status */ |
| 786 | xfs_inumbers( |
| 787 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 788 | xfs_ino_t *lastino, /* last inode returned */ |
| 789 | int *count, /* size of buffer/count returned */ |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 790 | void __user *ubuffer,/* buffer with inode descriptions */ |
| 791 | inumbers_fmt_pf formatter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
| 793 | xfs_buf_t *agbp; |
| 794 | xfs_agino_t agino; |
| 795 | xfs_agnumber_t agno; |
| 796 | int bcount; |
| 797 | xfs_inogrp_t *buffer; |
| 798 | int bufidx; |
| 799 | xfs_btree_cur_t *cur; |
| 800 | int error; |
| 801 | __int32_t gcnt; |
| 802 | xfs_inofree_t gfree; |
| 803 | xfs_agino_t gino; |
| 804 | int i; |
| 805 | xfs_ino_t ino; |
| 806 | int left; |
| 807 | int tmp; |
| 808 | |
| 809 | ino = (xfs_ino_t)*lastino; |
| 810 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 811 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 812 | left = *count; |
| 813 | *count = 0; |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 814 | bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP); |
| 816 | error = bufidx = 0; |
| 817 | cur = NULL; |
| 818 | agbp = NULL; |
| 819 | while (left > 0 && agno < mp->m_sb.sb_agcount) { |
| 820 | if (agbp == NULL) { |
| 821 | down_read(&mp->m_peraglock); |
| 822 | error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); |
| 823 | up_read(&mp->m_peraglock); |
| 824 | if (error) { |
| 825 | /* |
| 826 | * If we can't read the AGI of this ag, |
| 827 | * then just skip to the next one. |
| 828 | */ |
| 829 | ASSERT(cur == NULL); |
| 830 | agbp = NULL; |
| 831 | agno++; |
| 832 | agino = 0; |
| 833 | continue; |
| 834 | } |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 835 | cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp); |
| 837 | if (error) { |
| 838 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 839 | cur = NULL; |
| 840 | xfs_buf_relse(agbp); |
| 841 | agbp = NULL; |
| 842 | /* |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 843 | * Move up the last inode in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | * chunk. The lookup_ge will always get |
| 845 | * us the first inode in the next chunk. |
| 846 | */ |
| 847 | agino += XFS_INODES_PER_CHUNK - 1; |
| 848 | continue; |
| 849 | } |
| 850 | } |
| 851 | if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree, |
| 852 | &i)) || |
| 853 | i == 0) { |
| 854 | xfs_buf_relse(agbp); |
| 855 | agbp = NULL; |
| 856 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 857 | cur = NULL; |
| 858 | agno++; |
| 859 | agino = 0; |
| 860 | continue; |
| 861 | } |
| 862 | agino = gino + XFS_INODES_PER_CHUNK - 1; |
| 863 | buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino); |
| 864 | buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt; |
| 865 | buffer[bufidx].xi_allocmask = ~gfree; |
| 866 | bufidx++; |
| 867 | left--; |
| 868 | if (bufidx == bcount) { |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 869 | long written; |
| 870 | if (formatter(ubuffer, buffer, bufidx, &written)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | error = XFS_ERROR(EFAULT); |
| 872 | break; |
| 873 | } |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 874 | ubuffer += written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | *count += bufidx; |
| 876 | bufidx = 0; |
| 877 | } |
| 878 | if (left) { |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 879 | error = xfs_btree_increment(cur, 0, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | if (error) { |
| 881 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 882 | cur = NULL; |
| 883 | xfs_buf_relse(agbp); |
| 884 | agbp = NULL; |
| 885 | /* |
| 886 | * The agino value has already been bumped. |
| 887 | * Just try to skip up to it. |
| 888 | */ |
| 889 | agino += XFS_INODES_PER_CHUNK; |
| 890 | continue; |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | if (!error) { |
| 895 | if (bufidx) { |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 896 | long written; |
| 897 | if (formatter(ubuffer, buffer, bufidx, &written)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | error = XFS_ERROR(EFAULT); |
| 899 | else |
| 900 | *count += bufidx; |
| 901 | } |
| 902 | *lastino = XFS_AGINO_TO_INO(mp, agno, agino); |
| 903 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 904 | kmem_free(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | if (cur) |
| 906 | xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR : |
| 907 | XFS_BTREE_NOERROR)); |
| 908 | if (agbp) |
| 909 | xfs_buf_relse(agbp); |
| 910 | return error; |
| 911 | } |