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