1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/9p/vfs_dir.c 4 * 5 * This file contains vfs directory ops for the 9P2000 protocol. 6 * 7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> 8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> 9 */ 10 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/fs.h> 14 #include <linux/file.h> 15 #include <linux/stat.h> 16 #include <linux/string.h> 17 #include <linux/sched.h> 18 #include <linux/inet.h> 19 #include <linux/idr.h> 20 #include <linux/slab.h> 21 #include <linux/uio.h> 22 #include <net/9p/9p.h> 23 #include <net/9p/client.h> 24 25 #include "v9fs.h" 26 #include "v9fs_vfs.h" 27 #include "fid.h" 28 29 /** 30 * struct p9_rdir - readdir accounting 31 * @head: start offset of current dirread buffer 32 * @tail: end offset of current dirread buffer 33 * @buf: dirread buffer 34 * 35 * private structure for keeping track of readdir 36 * allocated on demand 37 */ 38 39 struct p9_rdir { 40 int head; 41 int tail; 42 uint8_t buf[]; 43 }; 44 45 /** 46 * dt_type - return file type 47 * @mistat: mistat structure 48 * 49 */ 50 51 static inline int dt_type(struct p9_wstat *mistat) 52 { 53 unsigned long perm = mistat->mode; 54 int rettype = DT_REG; 55 56 if (perm & P9_DMDIR) 57 rettype = DT_DIR; 58 if (perm & P9_DMSYMLINK) 59 rettype = DT_LNK; 60 61 return rettype; 62 } 63 64 /** 65 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir 66 * @filp: opened file structure 67 * @buflen: Length in bytes of buffer to allocate 68 * 69 */ 70 71 static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen) 72 { 73 struct p9_fid *fid = filp->private_data; 74 75 if (!fid->rdir) 76 fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL); 77 return fid->rdir; 78 } 79 80 /** 81 * v9fs_dir_readdir - iterate through a directory 82 * @file: opened file structure 83 * @ctx: actor we feed the entries to 84 * 85 */ 86 87 static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) 88 { 89 bool over; 90 struct p9_wstat st; 91 int err = 0; 92 struct p9_fid *fid; 93 int buflen; 94 struct p9_rdir *rdir; 95 struct kvec kvec; 96 97 p9_debug(P9_DEBUG_VFS, "name %pD\n", file); 98 fid = file->private_data; 99 100 buflen = fid->clnt->msize - P9_IOHDRSZ; 101 102 rdir = v9fs_alloc_rdir_buf(file, buflen); 103 if (!rdir) 104 return -ENOMEM; 105 kvec.iov_base = rdir->buf; 106 kvec.iov_len = buflen; 107 108 while (1) { 109 if (rdir->tail == rdir->head) { 110 struct iov_iter to; 111 int n; 112 113 iov_iter_kvec(&to, READ, &kvec, 1, buflen); 114 n = p9_client_read(file->private_data, ctx->pos, &to, 115 &err); 116 if (err) 117 return err; 118 if (n == 0) 119 return 0; 120 121 rdir->head = 0; 122 rdir->tail = n; 123 } 124 while (rdir->head < rdir->tail) { 125 err = p9stat_read(fid->clnt, rdir->buf + rdir->head, 126 rdir->tail - rdir->head, &st); 127 if (err <= 0) { 128 p9_debug(P9_DEBUG_VFS, "returned %d\n", err); 129 return -EIO; 130 } 131 132 over = !dir_emit(ctx, st.name, strlen(st.name), 133 v9fs_qid2ino(&st.qid), dt_type(&st)); 134 p9stat_free(&st); 135 if (over) 136 return 0; 137 138 rdir->head += err; 139 ctx->pos += err; 140 } 141 } 142 } 143 144 /** 145 * v9fs_dir_readdir_dotl - iterate through a directory 146 * @file: opened file structure 147 * @ctx: actor we feed the entries to 148 * 149 */ 150 static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx) 151 { 152 int err = 0; 153 struct p9_fid *fid; 154 int buflen; 155 struct p9_rdir *rdir; 156 struct p9_dirent curdirent; 157 158 p9_debug(P9_DEBUG_VFS, "name %pD\n", file); 159 fid = file->private_data; 160 161 buflen = fid->clnt->msize - P9_READDIRHDRSZ; 162 163 rdir = v9fs_alloc_rdir_buf(file, buflen); 164 if (!rdir) 165 return -ENOMEM; 166 167 while (1) { 168 if (rdir->tail == rdir->head) { 169 err = p9_client_readdir(fid, rdir->buf, buflen, 170 ctx->pos); 171 if (err <= 0) 172 return err; 173 174 rdir->head = 0; 175 rdir->tail = err; 176 } 177 178 while (rdir->head < rdir->tail) { 179 180 err = p9dirent_read(fid->clnt, rdir->buf + rdir->head, 181 rdir->tail - rdir->head, 182 &curdirent); 183 if (err < 0) { 184 p9_debug(P9_DEBUG_VFS, "returned %d\n", err); 185 return -EIO; 186 } 187 188 if (!dir_emit(ctx, curdirent.d_name, 189 strlen(curdirent.d_name), 190 v9fs_qid2ino(&curdirent.qid), 191 curdirent.d_type)) 192 return 0; 193 194 ctx->pos = curdirent.d_off; 195 rdir->head += err; 196 } 197 } 198 } 199 200 201 /** 202 * v9fs_dir_release - close a directory 203 * @inode: inode of the directory 204 * @filp: file pointer to a directory 205 * 206 */ 207 208 int v9fs_dir_release(struct inode *inode, struct file *filp) 209 { 210 struct p9_fid *fid; 211 212 fid = filp->private_data; 213 p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n", 214 inode, filp, fid ? fid->fid : -1); 215 if (fid) { 216 spin_lock(&inode->i_lock); 217 hlist_del(&fid->ilist); 218 spin_unlock(&inode->i_lock); 219 p9_client_clunk(fid); 220 } 221 return 0; 222 } 223 224 const struct file_operations v9fs_dir_operations = { 225 .read = generic_read_dir, 226 .llseek = generic_file_llseek, 227 .iterate_shared = v9fs_dir_readdir, 228 .open = v9fs_file_open, 229 .release = v9fs_dir_release, 230 }; 231 232 const struct file_operations v9fs_dir_operations_dotl = { 233 .read = generic_read_dir, 234 .llseek = generic_file_llseek, 235 .iterate_shared = v9fs_dir_readdir_dotl, 236 .open = v9fs_file_open, 237 .release = v9fs_dir_release, 238 .fsync = v9fs_file_fsync_dotl, 239 }; 240