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