Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2001 Christoph Hellwig. |
Krzysztof Błaszkowski | 1cce170 | 2016-06-01 09:25:12 +0200 | [diff] [blame] | 3 | * Copyright (c) 2016 Krzysztof Blaszkowski |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions, and the following disclaimer, |
| 11 | * without modification. |
| 12 | * 2. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 14 | * |
| 15 | * Alternatively, this software may be distributed under the terms of the |
| 16 | * GNU General Public License ("GPL"). |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR |
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Veritas filesystem driver - lookup and other directory related code. |
| 33 | */ |
| 34 | #include <linux/fs.h> |
| 35 | #include <linux/time.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/highmem.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include "vxfs.h" |
| 42 | #include "vxfs_dir.h" |
| 43 | #include "vxfs_inode.h" |
| 44 | #include "vxfs_extern.h" |
| 45 | |
| 46 | /* |
| 47 | * Number of VxFS blocks per page. |
| 48 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 49 | #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 52 | static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int); |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 53 | static int vxfs_readdir(struct file *, struct dir_context *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 55 | const struct inode_operations vxfs_dir_inode_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | .lookup = vxfs_lookup, |
| 57 | }; |
| 58 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 59 | const struct file_operations vxfs_dir_operations = { |
jan Blunck | ca57272 | 2010-05-26 14:44:53 -0700 | [diff] [blame] | 60 | .llseek = generic_file_llseek, |
| 61 | .read = generic_read_dir, |
Al Viro | c51da20 | 2016-04-30 22:37:34 -0400 | [diff] [blame] | 62 | .iterate_shared = vxfs_readdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * vxfs_find_entry - find a mathing directory entry for a dentry |
| 68 | * @ip: directory inode |
| 69 | * @dp: dentry for which we want to find a direct |
| 70 | * @ppp: gets filled with the page the return value sits in |
| 71 | * |
| 72 | * Description: |
| 73 | * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory |
| 74 | * cache entry @dp. @ppp will be filled with the page the return |
| 75 | * value resides in. |
| 76 | * |
| 77 | * Returns: |
| 78 | * The wanted direct on success, else a NULL pointer. |
| 79 | */ |
| 80 | static struct vxfs_direct * |
| 81 | vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp) |
| 82 | { |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 83 | u_long bsize = ip->i_sb->s_blocksize; |
| 84 | const char *name = dp->d_name.name; |
| 85 | int namelen = dp->d_name.len; |
| 86 | loff_t limit = VXFS_DIRROUND(ip->i_size); |
| 87 | struct vxfs_direct *de_exit = NULL; |
| 88 | loff_t pos = 0; |
| 89 | struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 91 | while (pos < limit) { |
| 92 | struct page *pp; |
| 93 | char *kaddr; |
| 94 | int pg_ofs = pos & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 96 | pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (IS_ERR(pp)) |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 98 | return NULL; |
| 99 | kaddr = (char *)page_address(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 101 | while (pg_ofs < PAGE_SIZE && pos < limit) { |
| 102 | struct vxfs_direct *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 104 | if ((pos & (bsize - 1)) < 4) { |
| 105 | struct vxfs_dirblk *dbp = |
| 106 | (struct vxfs_dirblk *) |
| 107 | (kaddr + (pos & ~PAGE_MASK)); |
| 108 | int overhead = VXFS_DIRBLKOV(sbi, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 110 | pos += overhead; |
| 111 | pg_ofs += overhead; |
| 112 | } |
| 113 | de = (struct vxfs_direct *)(kaddr + pg_ofs); |
| 114 | |
| 115 | if (!de->d_reclen) { |
| 116 | pos += bsize - 1; |
| 117 | pos &= ~(bsize - 1); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | pg_ofs += fs16_to_cpu(sbi, de->d_reclen); |
| 122 | pos += fs16_to_cpu(sbi, de->d_reclen); |
| 123 | if (!de->d_ino) |
| 124 | continue; |
| 125 | |
| 126 | if (namelen != fs16_to_cpu(sbi, de->d_namelen)) |
| 127 | continue; |
| 128 | if (!memcmp(name, de->d_name, namelen)) { |
| 129 | *ppp = pp; |
| 130 | de_exit = de; |
| 131 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 134 | if (!de_exit) |
| 135 | vxfs_put_page(pp); |
| 136 | else |
| 137 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 140 | return de_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * vxfs_inode_by_name - find inode number for dentry |
| 145 | * @dip: directory to search in |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 146 | * @dp: dentry we search for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | * |
| 148 | * Description: |
| 149 | * vxfs_inode_by_name finds out the inode number of |
| 150 | * the path component described by @dp in @dip. |
| 151 | * |
| 152 | * Returns: |
| 153 | * The wanted inode number on success, else Zero. |
| 154 | */ |
| 155 | static ino_t |
| 156 | vxfs_inode_by_name(struct inode *dip, struct dentry *dp) |
| 157 | { |
| 158 | struct vxfs_direct *de; |
| 159 | struct page *pp; |
| 160 | ino_t ino = 0; |
| 161 | |
| 162 | de = vxfs_find_entry(dip, dp, &pp); |
| 163 | if (de) { |
Krzysztof Błaszkowski | 0d83f7f | 2016-05-31 08:45:13 +0200 | [diff] [blame] | 164 | ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | kunmap(pp); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 166 | put_page(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return (ino); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * vxfs_lookup - lookup pathname component |
| 174 | * @dip: dir in which we lookup |
| 175 | * @dp: dentry we lookup |
Fabian Frederick | ddae82d | 2014-04-03 14:46:31 -0700 | [diff] [blame] | 176 | * @flags: lookup flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * |
| 178 | * Description: |
| 179 | * vxfs_lookup tries to lookup the pathname component described |
| 180 | * by @dp in @dip. |
| 181 | * |
| 182 | * Returns: |
Geert Uytterhoeven | eaf593c | 2015-05-21 14:12:29 +0200 | [diff] [blame] | 183 | * A NULL-pointer on success, else a negative error code encoded |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | * in the return pointer. |
| 185 | */ |
| 186 | static struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 187 | vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | struct inode *ip = NULL; |
| 190 | ino_t ino; |
| 191 | |
| 192 | if (dp->d_name.len > VXFS_NAMELEN) |
| 193 | return ERR_PTR(-ENAMETOOLONG); |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | ino = vxfs_inode_by_name(dip, dp); |
| 196 | if (ino) { |
David Howells | d0b0794 | 2008-02-07 00:15:39 -0800 | [diff] [blame] | 197 | ip = vxfs_iget(dip->i_sb, ino); |
Arnd Bergmann | 6d7bccc | 2010-10-21 15:58:01 +0200 | [diff] [blame] | 198 | if (IS_ERR(ip)) |
David Howells | d0b0794 | 2008-02-07 00:15:39 -0800 | [diff] [blame] | 199 | return ERR_CAST(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | d_add(dp, ip); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * vxfs_readdir - read a directory |
| 207 | * @fp: the directory to read |
| 208 | * @retp: return buffer |
| 209 | * @filler: filldir callback |
| 210 | * |
| 211 | * Description: |
| 212 | * vxfs_readdir fills @retp with directory entries from @fp |
| 213 | * using the VFS supplied callback @filler. |
| 214 | * |
| 215 | * Returns: |
| 216 | * Zero. |
| 217 | */ |
| 218 | static int |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 219 | vxfs_readdir(struct file *fp, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 221 | struct inode *ip = file_inode(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | struct super_block *sbp = ip->i_sb; |
| 223 | u_long bsize = sbp->s_blocksize; |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 224 | loff_t pos, limit; |
| 225 | struct vxfs_sb_info *sbi = VXFS_SBI(sbp); |
Krzysztof Błaszkowski | 0d83f7f | 2016-05-31 08:45:13 +0200 | [diff] [blame] | 226 | |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 227 | if (ctx->pos == 0) { |
| 228 | if (!dir_emit_dot(fp, ctx)) |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 229 | goto out; |
| 230 | ctx->pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 232 | if (ctx->pos == 1) { |
| 233 | if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR)) |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 234 | goto out; |
| 235 | ctx->pos++; |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 238 | limit = VXFS_DIRROUND(ip->i_size); |
| 239 | if (ctx->pos > limit) |
| 240 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 242 | pos = ctx->pos & ~3L; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 244 | while (pos < limit) { |
| 245 | struct page *pp; |
| 246 | char *kaddr; |
| 247 | int pg_ofs = pos & ~PAGE_MASK; |
| 248 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 250 | pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (IS_ERR(pp)) |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 252 | return -ENOMEM; |
| 253 | |
Al Viro | 9b5d5a1 | 2013-05-18 03:15:00 -0400 | [diff] [blame] | 254 | kaddr = (char *)page_address(pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 256 | while (pg_ofs < PAGE_SIZE && pos < limit) { |
| 257 | struct vxfs_direct *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 259 | if ((pos & (bsize - 1)) < 4) { |
| 260 | struct vxfs_dirblk *dbp = |
| 261 | (struct vxfs_dirblk *) |
| 262 | (kaddr + (pos & ~PAGE_MASK)); |
| 263 | int overhead = VXFS_DIRBLKOV(sbi, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 265 | pos += overhead; |
| 266 | pg_ofs += overhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 268 | de = (struct vxfs_direct *)(kaddr + pg_ofs); |
| 269 | |
| 270 | if (!de->d_reclen) { |
| 271 | pos += bsize - 1; |
| 272 | pos &= ~(bsize - 1); |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | pg_ofs += fs16_to_cpu(sbi, de->d_reclen); |
| 277 | pos += fs16_to_cpu(sbi, de->d_reclen); |
| 278 | if (!de->d_ino) |
| 279 | continue; |
| 280 | |
| 281 | rc = dir_emit(ctx, de->d_name, |
| 282 | fs16_to_cpu(sbi, de->d_namelen), |
| 283 | fs32_to_cpu(sbi, de->d_ino), |
| 284 | DT_UNKNOWN); |
| 285 | if (!rc) { |
| 286 | /* the dir entry was not read, fix pos. */ |
| 287 | pos -= fs16_to_cpu(sbi, de->d_reclen); |
| 288 | break; |
| 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | vxfs_put_page(pp); |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 292 | if (!rc) |
| 293 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
Krzysztof Błaszkowski | 12495ea | 2016-06-12 19:26:45 +0200 | [diff] [blame] | 295 | |
| 296 | ctx->pos = pos | 2; |
| 297 | |
| 298 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |