blob: 04a3dd84c99340d6875a97702bd3ad5577553e83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * Directory operations for Coda filesystem
4 * Original version: (C) 1996 P. Braam and M. Callahan
5 * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
6 *
7 * Carnegie Mellon encourages users to contribute improvements to
8 * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
9 */
10
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/time.h>
14#include <linux/fs.h>
15#include <linux/file.h>
16#include <linux/stat.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/smp_lock.h>
20
21#include <asm/uaccess.h>
22
23#include <linux/coda.h>
24#include <linux/coda_linux.h>
25#include <linux/coda_psdev.h>
26#include <linux/coda_fs_i.h>
27#include <linux/coda_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Adrian Bunkc98d8cf2006-03-24 03:15:53 -080029#include "coda_int.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* dir inode-ops */
32static int coda_create(struct inode *dir, struct dentry *new, int mode, struct nameidata *nd);
33static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
34static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
35 struct dentry *entry);
36static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
37static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
38 const char *symname);
39static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
40static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
41static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
42 struct inode *new_inode, struct dentry *new_dentry);
43
44/* dir file-ops */
Jan Harkes97875252007-07-19 01:48:47 -070045static int coda_readdir(struct file *file, void *buf, filldir_t filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* dentry ops */
48static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
49static int coda_dentry_delete(struct dentry *);
50
51/* support routines */
Jan Harkes97875252007-07-19 01:48:47 -070052static int coda_venus_readdir(struct file *coda_file, void *buf,
53 filldir_t filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* same as fs/bad_inode.c */
56static int coda_return_EIO(void)
57{
58 return -EIO;
59}
60#define CODA_EIO_ERROR ((void *) (coda_return_EIO))
61
62static struct dentry_operations coda_dentry_operations =
63{
64 .d_revalidate = coda_dentry_revalidate,
65 .d_delete = coda_dentry_delete,
66};
67
Arjan van de Ven754661f2007-02-12 00:55:38 -080068const struct inode_operations coda_dir_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 .create = coda_create,
71 .lookup = coda_lookup,
72 .link = coda_link,
73 .unlink = coda_unlink,
74 .symlink = coda_symlink,
75 .mkdir = coda_mkdir,
76 .rmdir = coda_rmdir,
77 .mknod = CODA_EIO_ERROR,
78 .rename = coda_rename,
79 .permission = coda_permission,
80 .getattr = coda_getattr,
81 .setattr = coda_setattr,
82};
83
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080084const struct file_operations coda_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .llseek = generic_file_llseek,
86 .read = generic_read_dir,
87 .readdir = coda_readdir,
88 .open = coda_open,
89 .flush = coda_flush,
90 .release = coda_release,
91 .fsync = coda_fsync,
92};
93
94
95/* inode operations for directories */
96/* access routines: lookup, readlink, permission */
97static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
98{
Jan Harkesed36f722007-07-19 01:48:49 -070099 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct CodaFid resfid = { { 0, } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int type = 0;
102 int error = 0;
103 const char *name = entry->d_name.name;
104 size_t length = entry->d_name.len;
Jan Harkesed36f722007-07-19 01:48:49 -0700105
106 if (length > CODA_MAXNAMLEN) {
107 printk(KERN_ERR "name too long: lookup, %s (%*s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 coda_i2s(dir), (int)length, name);
109 return ERR_PTR(-ENAMETOOLONG);
110 }
111
Jan Harkesed36f722007-07-19 01:48:49 -0700112 /* control object, create inode on the fly */
113 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
114 error = coda_cnode_makectl(&inode, dir->i_sb);
115 type = CODA_NOCACHE;
116 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
Jan Harkesed36f722007-07-19 01:48:49 -0700119 lock_kernel();
120
121 error = venus_lookup(dir->i_sb, coda_i2f(dir), name, length,
122 &type, &resfid);
123 if (!error)
124 error = coda_cnode_make(&inode, &resfid, dir->i_sb);
125
126 unlock_kernel();
127
128 if (error && error != -ENOENT)
129 return ERR_PTR(error);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 entry->d_op = &coda_dentry_operations;
Jan Harkesed36f722007-07-19 01:48:49 -0700133
134 if (inode && (type & CODA_NOCACHE))
135 coda_flag_inode(inode, C_VATTR | C_PURGE);
136
137 return d_splice_alias(inode, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140
141int coda_permission(struct inode *inode, int mask, struct nameidata *nd)
142{
143 int error = 0;
144
145 if (!mask)
146 return 0;
147
148 lock_kernel();
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (coda_cache_check(inode, mask))
151 goto out;
152
153 error = venus_access(inode->i_sb, coda_i2f(inode), mask);
154
155 if (!error)
156 coda_cache_enter(inode, mask);
157
158 out:
159 unlock_kernel();
Jan Harkesd7289002007-07-19 01:48:43 -0700160 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163
Jan Harkesd7289002007-07-19 01:48:43 -0700164static inline void coda_dir_update_mtime(struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166#ifdef REQUERY_VENUS_FOR_MTIME
167 /* invalidate the directory cnode's attributes so we refetch the
168 * attributes from venus next time the inode is referenced */
169 coda_flag_inode(dir, C_VATTR);
170#else
171 /* optimistically we can also act as if our nose bleeds. The
Jan Harkesd7289002007-07-19 01:48:43 -0700172 * granularity of the mtime is coarse anyways so we might actually be
173 * right most of the time. Note: we only do this for directories. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
175#endif
Jan Harkesd7289002007-07-19 01:48:43 -0700176}
177
178/* we have to wrap inc_nlink/drop_nlink because sometimes userspace uses a
179 * trick to fool GNU find's optimizations. If we can't be sure of the link
180 * (because of volume mount points) we set i_nlink to 1 which forces find
181 * to consider every child as a possible directory. We should also never
182 * see an increment or decrement for deleted directories where i_nlink == 0 */
183static inline void coda_dir_inc_nlink(struct inode *dir)
184{
185 if (dir->i_nlink >= 2)
186 inc_nlink(dir);
187}
188
189static inline void coda_dir_drop_nlink(struct inode *dir)
190{
191 if (dir->i_nlink > 2)
192 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/* creation routines: create, mknod, mkdir, link, symlink */
196static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
197{
198 int error=0;
199 const char *name=de->d_name.name;
200 int length=de->d_name.len;
201 struct inode *inode;
202 struct CodaFid newfid;
203 struct coda_vattr attrs;
204
205 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (coda_isroot(dir) && coda_iscontrol(name, length)) {
208 unlock_kernel();
209 return -EPERM;
210 }
211
212 error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
213 0, mode, &newfid, &attrs);
214
215 if ( error ) {
216 unlock_kernel();
217 d_drop(de);
218 return error;
219 }
220
221 inode = coda_iget(dir->i_sb, &newfid, &attrs);
222 if ( IS_ERR(inode) ) {
223 unlock_kernel();
224 d_drop(de);
225 return PTR_ERR(inode);
226 }
227
228 /* invalidate the directory cnode's attributes */
Jan Harkesd7289002007-07-19 01:48:43 -0700229 coda_dir_update_mtime(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unlock_kernel();
231 d_instantiate(de, inode);
Jan Harkesd7289002007-07-19 01:48:43 -0700232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
236{
237 struct inode *inode;
238 struct coda_vattr attrs;
239 const char *name = de->d_name.name;
240 int len = de->d_name.len;
241 int error;
242 struct CodaFid newfid;
243
244 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (coda_isroot(dir) && coda_iscontrol(name, len)) {
247 unlock_kernel();
248 return -EPERM;
249 }
250
251 attrs.va_mode = mode;
252 error = venus_mkdir(dir->i_sb, coda_i2f(dir),
253 name, len, &newfid, &attrs);
254
255 if ( error ) {
256 unlock_kernel();
257 d_drop(de);
258 return error;
259 }
260
261 inode = coda_iget(dir->i_sb, &newfid, &attrs);
262 if ( IS_ERR(inode) ) {
263 unlock_kernel();
264 d_drop(de);
265 return PTR_ERR(inode);
266 }
Jan Harkesd7289002007-07-19 01:48:43 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* invalidate the directory cnode's attributes */
Jan Harkesd7289002007-07-19 01:48:43 -0700269 coda_dir_inc_nlink(dir);
270 coda_dir_update_mtime(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unlock_kernel();
272 d_instantiate(de, inode);
Jan Harkesd7289002007-07-19 01:48:43 -0700273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/* try to make de an entry in dir_inodde linked to source_de */
277static int coda_link(struct dentry *source_de, struct inode *dir_inode,
278 struct dentry *de)
279{
280 struct inode *inode = source_de->d_inode;
281 const char * name = de->d_name.name;
282 int len = de->d_name.len;
283 int error;
284
285 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
288 unlock_kernel();
289 return -EPERM;
290 }
291
292 error = venus_link(dir_inode->i_sb, coda_i2f(inode),
293 coda_i2f(dir_inode), (const char *)name, len);
294
Jan Harkesd7289002007-07-19 01:48:43 -0700295 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 d_drop(de);
297 goto out;
298 }
299
Jan Harkesd7289002007-07-19 01:48:43 -0700300 coda_dir_update_mtime(dir_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 atomic_inc(&inode->i_count);
302 d_instantiate(de, inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700303 inc_nlink(inode);
Jan Harkesd7289002007-07-19 01:48:43 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305out:
306 unlock_kernel();
307 return(error);
308}
309
310
311static int coda_symlink(struct inode *dir_inode, struct dentry *de,
312 const char *symname)
313{
314 const char *name = de->d_name.name;
315 int len = de->d_name.len;
316 int symlen;
Jan Harkes3cf01f22007-07-19 01:48:51 -0700317 int error = 0;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
322 unlock_kernel();
323 return -EPERM;
324 }
325
326 symlen = strlen(symname);
327 if ( symlen > CODA_MAXPATHLEN ) {
328 unlock_kernel();
329 return -ENAMETOOLONG;
330 }
331
332 /*
333 * This entry is now negative. Since we do not create
Jan Harkesd7289002007-07-19 01:48:43 -0700334 * an inode for the entry we have to drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336 d_drop(de);
Jan Harkesd7289002007-07-19 01:48:43 -0700337 error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 symname, symlen);
339
340 /* mtime is no good anymore */
341 if ( !error )
Jan Harkesd7289002007-07-19 01:48:43 -0700342 coda_dir_update_mtime(dir_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 unlock_kernel();
Jan Harkesd7289002007-07-19 01:48:43 -0700345 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/* destruction routines: unlink, rmdir */
349int coda_unlink(struct inode *dir, struct dentry *de)
350{
351 int error;
352 const char *name = de->d_name.name;
353 int len = de->d_name.len;
354
355 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jan Harkesd7289002007-07-19 01:48:43 -0700357 error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
358 if ( error ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unlock_kernel();
Jan Harkesd7289002007-07-19 01:48:43 -0700360 return error;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jan Harkesd7289002007-07-19 01:48:43 -0700363 coda_dir_update_mtime(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700364 drop_nlink(de->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unlock_kernel();
Jan Harkesd7289002007-07-19 01:48:43 -0700366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369int coda_rmdir(struct inode *dir, struct dentry *de)
370{
371 const char *name = de->d_name.name;
372 int len = de->d_name.len;
Jan Harkes8c6d2152007-07-19 01:48:43 -0700373 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
Jan Harkes8c6d2152007-07-19 01:48:43 -0700378 if (!error) {
379 /* VFS may delete the child */
380 if (de->d_inode)
381 de->d_inode->i_nlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jan Harkes8c6d2152007-07-19 01:48:43 -0700383 /* fix the link count of the parent */
384 coda_dir_drop_nlink(dir);
385 coda_dir_update_mtime(dir);
Jan Harkesd7289002007-07-19 01:48:43 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unlock_kernel();
Jan Harkes8c6d2152007-07-19 01:48:43 -0700388 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/* rename */
Jan Harkesd7289002007-07-19 01:48:43 -0700392static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct inode *new_dir, struct dentry *new_dentry)
394{
Jan Harkesd7289002007-07-19 01:48:43 -0700395 const char *old_name = old_dentry->d_name.name;
396 const char *new_name = new_dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int old_length = old_dentry->d_name.len;
398 int new_length = new_dentry->d_name.len;
Jan Harkesd7289002007-07-19 01:48:43 -0700399 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jan Harkesd7289002007-07-19 01:48:43 -0700403 error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
404 coda_i2f(new_dir), old_length, new_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 (const char *) old_name, (const char *)new_name);
406
Jan Harkesd7289002007-07-19 01:48:43 -0700407 if ( !error ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if ( new_dentry->d_inode ) {
Jan Harkesd7289002007-07-19 01:48:43 -0700409 if ( S_ISDIR(new_dentry->d_inode->i_mode) ) {
410 coda_dir_drop_nlink(old_dir);
411 coda_dir_inc_nlink(new_dir);
412 }
413 coda_dir_update_mtime(old_dir);
414 coda_dir_update_mtime(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 coda_flag_inode(new_dentry->d_inode, C_VATTR);
416 } else {
417 coda_flag_inode(old_dir, C_VATTR);
418 coda_flag_inode(new_dir, C_VATTR);
Jan Harkesd7289002007-07-19 01:48:43 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 unlock_kernel();
422
423 return error;
424}
425
426
427/* file operations for directories */
Jan Harkes97875252007-07-19 01:48:47 -0700428int coda_readdir(struct file *coda_file, void *buf, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct coda_file_info *cfi;
431 struct file *host_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int ret;
433
434 cfi = CODA_FTOC(coda_file);
435 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
436 host_file = cfi->cfi_container;
437
Jan Harkes97875252007-07-19 01:48:47 -0700438 if (!host_file->f_op)
439 return -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Jan Harkes97875252007-07-19 01:48:47 -0700441 if (host_file->f_op->readdir)
442 {
443 /* potemkin case: we were handed a directory inode.
444 * We can't use vfs_readdir because we have to keep the file
445 * position in sync between the coda_file and the host_file.
446 * and as such we need grab the inode mutex. */
447 struct inode *host_inode = host_file->f_path.dentry->d_inode;
448
449 mutex_lock(&host_inode->i_mutex);
450 host_file->f_pos = coda_file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 ret = -ENOENT;
453 if (!IS_DEADDIR(host_inode)) {
Jan Harkes97875252007-07-19 01:48:47 -0700454 ret = host_file->f_op->readdir(host_file, buf, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 file_accessed(host_file);
456 }
Jan Harkes97875252007-07-19 01:48:47 -0700457
458 coda_file->f_pos = host_file->f_pos;
459 mutex_unlock(&host_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Jan Harkes97875252007-07-19 01:48:47 -0700461 else /* Venus: we must read Venus dirents from a file */
462 ret = coda_venus_readdir(coda_file, buf, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 return ret;
465}
466
467static inline unsigned int CDT2DT(unsigned char cdt)
468{
469 unsigned int dt;
470
471 switch(cdt) {
472 case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
473 case CDT_FIFO: dt = DT_FIFO; break;
474 case CDT_CHR: dt = DT_CHR; break;
475 case CDT_DIR: dt = DT_DIR; break;
476 case CDT_BLK: dt = DT_BLK; break;
477 case CDT_REG: dt = DT_REG; break;
478 case CDT_LNK: dt = DT_LNK; break;
479 case CDT_SOCK: dt = DT_SOCK; break;
480 case CDT_WHT: dt = DT_WHT; break;
481 default: dt = DT_UNKNOWN; break;
482 }
483 return dt;
484}
485
486/* support routines */
Jan Harkes97875252007-07-19 01:48:47 -0700487static int coda_venus_readdir(struct file *coda_file, void *buf,
488 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int result = 0; /* # of entries returned */
Jan Harkes97875252007-07-19 01:48:47 -0700491 struct coda_file_info *cfi;
492 struct coda_inode_info *cii;
493 struct file *host_file;
494 struct dentry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct venus_dirent *vdir;
496 unsigned long vdir_size =
497 (unsigned long)(&((struct venus_dirent *)0)->d_name);
498 unsigned int type;
499 struct qstr name;
500 ino_t ino;
Jan Harkes97875252007-07-19 01:48:47 -0700501 int ret;
502
503 cfi = CODA_FTOC(coda_file);
504 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
505 host_file = cfi->cfi_container;
506
507 de = coda_file->f_path.dentry;
508 cii = ITOC(de->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700510 vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!vdir) return -ENOMEM;
512
Jan Harkes97875252007-07-19 01:48:47 -0700513 switch (coda_file->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case 0:
Jan Harkes97875252007-07-19 01:48:47 -0700515 ret = filldir(buf, ".", 1, 0, de->d_inode->i_ino, DT_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (ret < 0) break;
517 result++;
Jan Harkes97875252007-07-19 01:48:47 -0700518 coda_file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* fallthrough */
520 case 1:
Jan Harkes97875252007-07-19 01:48:47 -0700521 ret = filldir(buf, "..", 2, 1, de->d_parent->d_inode->i_ino, DT_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (ret < 0) break;
523 result++;
Jan Harkes97875252007-07-19 01:48:47 -0700524 coda_file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* fallthrough */
526 default:
527 while (1) {
528 /* read entries from the directory file */
Jan Harkes97875252007-07-19 01:48:47 -0700529 ret = kernel_read(host_file, coda_file->f_pos - 2, (char *)vdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sizeof(*vdir));
531 if (ret < 0) {
Jan Harkes97875252007-07-19 01:48:47 -0700532 printk(KERN_ERR "coda readdir: read dir %s failed %d\n",
533 coda_f2s(&cii->c_fid), ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 break;
535 }
536 if (ret == 0) break; /* end of directory file reached */
537
538 /* catch truncated reads */
539 if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
Jan Harkes97875252007-07-19 01:48:47 -0700540 printk(KERN_ERR "coda readdir: short read on %s\n",
541 coda_f2s(&cii->c_fid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ret = -EBADF;
543 break;
544 }
545 /* validate whether the directory file actually makes sense */
546 if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
Jan Harkes97875252007-07-19 01:48:47 -0700547 printk(KERN_ERR "coda readdir: invalid dir %s\n",
548 coda_f2s(&cii->c_fid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ret = -EBADF;
550 break;
551 }
552
553 name.len = vdir->d_namlen;
554 name.name = vdir->d_name;
555
556 /* Make sure we skip '.' and '..', we already got those */
557 if (name.name[0] == '.' && (name.len == 1 ||
558 (vdir->d_name[1] == '.' && name.len == 2)))
559 vdir->d_fileno = name.len = 0;
560
561 /* skip null entries */
562 if (vdir->d_fileno && name.len) {
563 /* try to look up this entry in the dcache, that way
564 * userspace doesn't have to worry about breaking
565 * getcwd by having mismatched inode numbers for
566 * internal volume mountpoints. */
Jan Harkes97875252007-07-19 01:48:47 -0700567 ino = find_inode_number(de, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!ino) ino = vdir->d_fileno;
569
570 type = CDT2DT(vdir->d_type);
Jan Harkes97875252007-07-19 01:48:47 -0700571 ret = filldir(buf, name.name, name.len,
572 coda_file->f_pos, ino, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* failure means no space for filling in this round */
574 if (ret < 0) break;
575 result++;
576 }
577 /* we'll always have progress because d_reclen is unsigned and
578 * we've already established it is non-zero. */
Jan Harkes97875252007-07-19 01:48:47 -0700579 coda_file->f_pos += vdir->d_reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Jan Harkes97875252007-07-19 01:48:47 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(vdir);
583 return result ? result : ret;
584}
585
586/* called when a cache lookup succeeds */
587static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
588{
589 struct inode *inode = de->d_inode;
590 struct coda_inode_info *cii;
591
592 if (!inode)
593 return 1;
594 lock_kernel();
595 if (coda_isroot(inode))
596 goto out;
597 if (is_bad_inode(inode))
598 goto bad;
599
600 cii = ITOC(de->d_inode);
601 if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
602 goto out;
603
604 shrink_dcache_parent(de);
605
606 /* propagate for a flush */
607 if (cii->c_flags & C_FLUSH)
608 coda_flag_inode_children(inode, C_FLUSH);
609
610 if (atomic_read(&de->d_count) > 1)
611 /* pretend it's valid, but don't change the flags */
612 goto out;
613
614 /* clear the flags. */
615 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
616
617bad:
618 unlock_kernel();
619 return 0;
620out:
621 unlock_kernel();
622 return 1;
623}
624
625/*
626 * This is the callback from dput() when d_count is going to 0.
627 * We use this to unhash dentries with bad inodes.
628 */
629static int coda_dentry_delete(struct dentry * dentry)
630{
631 int flags;
632
633 if (!dentry->d_inode)
634 return 0;
635
636 flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
637 if (is_bad_inode(dentry->d_inode) || flags) {
638 return 1;
639 }
640 return 0;
641}
642
643
644
645/*
646 * This is called when we want to check if the inode has
647 * changed on the server. Coda makes this easy since the
648 * cache manager Venus issues a downcall to the kernel when this
649 * happens
650 */
651int coda_revalidate_inode(struct dentry *dentry)
652{
653 struct coda_vattr attr;
654 int error = 0;
655 int old_mode;
656 ino_t old_ino;
657 struct inode *inode = dentry->d_inode;
658 struct coda_inode_info *cii = ITOC(inode);
659
660 lock_kernel();
661 if ( !cii->c_flags )
662 goto ok;
663
664 if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
665 error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
666 if ( error )
667 goto return_bad;
668
669 /* this inode may be lost if:
670 - it's ino changed
671 - type changes must be permitted for repair and
672 missing mount points.
673 */
674 old_mode = inode->i_mode;
675 old_ino = inode->i_ino;
676 coda_vattr_to_iattr(inode, &attr);
677
678 if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
679 printk("Coda: inode %ld, fid %s changed type!\n",
680 inode->i_ino, coda_f2s(&(cii->c_fid)));
681 }
682
683 /* the following can happen when a local fid is replaced
684 with a global one, here we lose and declare the inode bad */
685 if (inode->i_ino != old_ino)
686 goto return_bad;
687
688 coda_flag_inode_children(inode, C_FLUSH);
689 cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
690 }
691
692ok:
693 unlock_kernel();
694 return 0;
695
696return_bad:
697 unlock_kernel();
698 return -EIO;
699}