dir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Directory operations for Coda filesystem
  3. * Original version: (C) 1996 P. Braam and M. Callahan
  4. * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
  5. *
  6. * Carnegie Mellon encourages users to contribute improvements to
  7. * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/time.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/file.h>
  15. #include <linux/stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/namei.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/coda.h>
  22. #include <linux/coda_psdev.h>
  23. #include "coda_linux.h"
  24. #include "coda_cache.h"
  25. #include "coda_int.h"
  26. /* dir inode-ops */
  27. static int coda_create(struct inode *dir, struct dentry *new, umode_t mode, bool excl);
  28. static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, unsigned int flags);
  29. static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
  30. struct dentry *entry);
  31. static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
  32. static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
  33. const char *symname);
  34. static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, umode_t mode);
  35. static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
  36. static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
  37. struct inode *new_inode, struct dentry *new_dentry);
  38. /* dir file-ops */
  39. static int coda_readdir(struct file *file, struct dir_context *ctx);
  40. /* dentry ops */
  41. static int coda_dentry_revalidate(struct dentry *de, unsigned int flags);
  42. static int coda_dentry_delete(const struct dentry *);
  43. /* support routines */
  44. static int coda_venus_readdir(struct file *, struct dir_context *);
  45. /* same as fs/bad_inode.c */
  46. static int coda_return_EIO(void)
  47. {
  48. return -EIO;
  49. }
  50. #define CODA_EIO_ERROR ((void *) (coda_return_EIO))
  51. const struct dentry_operations coda_dentry_operations =
  52. {
  53. .d_revalidate = coda_dentry_revalidate,
  54. .d_delete = coda_dentry_delete,
  55. };
  56. const struct inode_operations coda_dir_inode_operations =
  57. {
  58. .create = coda_create,
  59. .lookup = coda_lookup,
  60. .link = coda_link,
  61. .unlink = coda_unlink,
  62. .symlink = coda_symlink,
  63. .mkdir = coda_mkdir,
  64. .rmdir = coda_rmdir,
  65. .mknod = CODA_EIO_ERROR,
  66. .rename = coda_rename,
  67. .permission = coda_permission,
  68. .getattr = coda_getattr,
  69. .setattr = coda_setattr,
  70. };
  71. const struct file_operations coda_dir_operations = {
  72. .llseek = generic_file_llseek,
  73. .read = generic_read_dir,
  74. .iterate = coda_readdir,
  75. .open = coda_open,
  76. .release = coda_release,
  77. .fsync = coda_fsync,
  78. };
  79. /* inode operations for directories */
  80. /* access routines: lookup, readlink, permission */
  81. static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, unsigned int flags)
  82. {
  83. struct super_block *sb = dir->i_sb;
  84. const char *name = entry->d_name.name;
  85. size_t length = entry->d_name.len;
  86. struct inode *inode;
  87. int type = 0;
  88. if (length > CODA_MAXNAMLEN) {
  89. printk(KERN_ERR "name too long: lookup, %s (%*s)\n",
  90. coda_i2s(dir), (int)length, name);
  91. return ERR_PTR(-ENAMETOOLONG);
  92. }
  93. /* control object, create inode on the fly */
  94. if (coda_isroot(dir) && coda_iscontrol(name, length)) {
  95. inode = coda_cnode_makectl(sb);
  96. type = CODA_NOCACHE;
  97. } else {
  98. struct CodaFid fid = { { 0, } };
  99. int error = venus_lookup(sb, coda_i2f(dir), name, length,
  100. &type, &fid);
  101. inode = !error ? coda_cnode_make(&fid, sb) : ERR_PTR(error);
  102. }
  103. if (!IS_ERR(inode) && (type & CODA_NOCACHE))
  104. coda_flag_inode(inode, C_VATTR | C_PURGE);
  105. if (inode == ERR_PTR(-ENOENT))
  106. inode = NULL;
  107. return d_splice_alias(inode, entry);
  108. }
  109. int coda_permission(struct inode *inode, int mask)
  110. {
  111. int error;
  112. if (mask & MAY_NOT_BLOCK)
  113. return -ECHILD;
  114. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  115. if (!mask)
  116. return 0;
  117. if ((mask & MAY_EXEC) && !execute_ok(inode))
  118. return -EACCES;
  119. if (coda_cache_check(inode, mask))
  120. return 0;
  121. error = venus_access(inode->i_sb, coda_i2f(inode), mask);
  122. if (!error)
  123. coda_cache_enter(inode, mask);
  124. return error;
  125. }
  126. static inline void coda_dir_update_mtime(struct inode *dir)
  127. {
  128. #ifdef REQUERY_VENUS_FOR_MTIME
  129. /* invalidate the directory cnode's attributes so we refetch the
  130. * attributes from venus next time the inode is referenced */
  131. coda_flag_inode(dir, C_VATTR);
  132. #else
  133. /* optimistically we can also act as if our nose bleeds. The
  134. * granularity of the mtime is coarse anyways so we might actually be
  135. * right most of the time. Note: we only do this for directories. */
  136. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  137. #endif
  138. }
  139. /* we have to wrap inc_nlink/drop_nlink because sometimes userspace uses a
  140. * trick to fool GNU find's optimizations. If we can't be sure of the link
  141. * (because of volume mount points) we set i_nlink to 1 which forces find
  142. * to consider every child as a possible directory. We should also never
  143. * see an increment or decrement for deleted directories where i_nlink == 0 */
  144. static inline void coda_dir_inc_nlink(struct inode *dir)
  145. {
  146. if (dir->i_nlink >= 2)
  147. inc_nlink(dir);
  148. }
  149. static inline void coda_dir_drop_nlink(struct inode *dir)
  150. {
  151. if (dir->i_nlink > 2)
  152. drop_nlink(dir);
  153. }
  154. /* creation routines: create, mknod, mkdir, link, symlink */
  155. static int coda_create(struct inode *dir, struct dentry *de, umode_t mode, bool excl)
  156. {
  157. int error;
  158. const char *name=de->d_name.name;
  159. int length=de->d_name.len;
  160. struct inode *inode;
  161. struct CodaFid newfid;
  162. struct coda_vattr attrs;
  163. if (coda_isroot(dir) && coda_iscontrol(name, length))
  164. return -EPERM;
  165. error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
  166. 0, mode, &newfid, &attrs);
  167. if (error)
  168. goto err_out;
  169. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  170. if (IS_ERR(inode)) {
  171. error = PTR_ERR(inode);
  172. goto err_out;
  173. }
  174. /* invalidate the directory cnode's attributes */
  175. coda_dir_update_mtime(dir);
  176. d_instantiate(de, inode);
  177. return 0;
  178. err_out:
  179. d_drop(de);
  180. return error;
  181. }
  182. static int coda_mkdir(struct inode *dir, struct dentry *de, umode_t mode)
  183. {
  184. struct inode *inode;
  185. struct coda_vattr attrs;
  186. const char *name = de->d_name.name;
  187. int len = de->d_name.len;
  188. int error;
  189. struct CodaFid newfid;
  190. if (coda_isroot(dir) && coda_iscontrol(name, len))
  191. return -EPERM;
  192. attrs.va_mode = mode;
  193. error = venus_mkdir(dir->i_sb, coda_i2f(dir),
  194. name, len, &newfid, &attrs);
  195. if (error)
  196. goto err_out;
  197. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  198. if (IS_ERR(inode)) {
  199. error = PTR_ERR(inode);
  200. goto err_out;
  201. }
  202. /* invalidate the directory cnode's attributes */
  203. coda_dir_inc_nlink(dir);
  204. coda_dir_update_mtime(dir);
  205. d_instantiate(de, inode);
  206. return 0;
  207. err_out:
  208. d_drop(de);
  209. return error;
  210. }
  211. /* try to make de an entry in dir_inodde linked to source_de */
  212. static int coda_link(struct dentry *source_de, struct inode *dir_inode,
  213. struct dentry *de)
  214. {
  215. struct inode *inode = source_de->d_inode;
  216. const char * name = de->d_name.name;
  217. int len = de->d_name.len;
  218. int error;
  219. if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
  220. return -EPERM;
  221. error = venus_link(dir_inode->i_sb, coda_i2f(inode),
  222. coda_i2f(dir_inode), (const char *)name, len);
  223. if (error) {
  224. d_drop(de);
  225. return error;
  226. }
  227. coda_dir_update_mtime(dir_inode);
  228. ihold(inode);
  229. d_instantiate(de, inode);
  230. inc_nlink(inode);
  231. return 0;
  232. }
  233. static int coda_symlink(struct inode *dir_inode, struct dentry *de,
  234. const char *symname)
  235. {
  236. const char *name = de->d_name.name;
  237. int len = de->d_name.len;
  238. int symlen;
  239. int error;
  240. if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
  241. return -EPERM;
  242. symlen = strlen(symname);
  243. if (symlen > CODA_MAXPATHLEN)
  244. return -ENAMETOOLONG;
  245. /*
  246. * This entry is now negative. Since we do not create
  247. * an inode for the entry we have to drop it.
  248. */
  249. d_drop(de);
  250. error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
  251. symname, symlen);
  252. /* mtime is no good anymore */
  253. if (!error)
  254. coda_dir_update_mtime(dir_inode);
  255. return error;
  256. }
  257. /* destruction routines: unlink, rmdir */
  258. static int coda_unlink(struct inode *dir, struct dentry *de)
  259. {
  260. int error;
  261. const char *name = de->d_name.name;
  262. int len = de->d_name.len;
  263. error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
  264. if (error)
  265. return error;
  266. coda_dir_update_mtime(dir);
  267. drop_nlink(de->d_inode);
  268. return 0;
  269. }
  270. static int coda_rmdir(struct inode *dir, struct dentry *de)
  271. {
  272. const char *name = de->d_name.name;
  273. int len = de->d_name.len;
  274. int error;
  275. error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
  276. if (!error) {
  277. /* VFS may delete the child */
  278. if (de->d_inode)
  279. clear_nlink(de->d_inode);
  280. /* fix the link count of the parent */
  281. coda_dir_drop_nlink(dir);
  282. coda_dir_update_mtime(dir);
  283. }
  284. return error;
  285. }
  286. /* rename */
  287. static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
  288. struct inode *new_dir, struct dentry *new_dentry)
  289. {
  290. const char *old_name = old_dentry->d_name.name;
  291. const char *new_name = new_dentry->d_name.name;
  292. int old_length = old_dentry->d_name.len;
  293. int new_length = new_dentry->d_name.len;
  294. int error;
  295. error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
  296. coda_i2f(new_dir), old_length, new_length,
  297. (const char *) old_name, (const char *)new_name);
  298. if (!error) {
  299. if (new_dentry->d_inode) {
  300. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  301. coda_dir_drop_nlink(old_dir);
  302. coda_dir_inc_nlink(new_dir);
  303. }
  304. coda_dir_update_mtime(old_dir);
  305. coda_dir_update_mtime(new_dir);
  306. coda_flag_inode(new_dentry->d_inode, C_VATTR);
  307. } else {
  308. coda_flag_inode(old_dir, C_VATTR);
  309. coda_flag_inode(new_dir, C_VATTR);
  310. }
  311. }
  312. return error;
  313. }
  314. /* file operations for directories */
  315. static int coda_readdir(struct file *coda_file, struct dir_context *ctx)
  316. {
  317. struct coda_file_info *cfi;
  318. struct file *host_file;
  319. int ret;
  320. cfi = CODA_FTOC(coda_file);
  321. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  322. host_file = cfi->cfi_container;
  323. if (!host_file->f_op)
  324. return -ENOTDIR;
  325. if (host_file->f_op->iterate) {
  326. struct inode *host_inode = file_inode(host_file);
  327. mutex_lock(&host_inode->i_mutex);
  328. ret = -ENOENT;
  329. if (!IS_DEADDIR(host_inode)) {
  330. ret = host_file->f_op->iterate(host_file, ctx);
  331. file_accessed(host_file);
  332. }
  333. mutex_unlock(&host_inode->i_mutex);
  334. return ret;
  335. }
  336. /* Venus: we must read Venus dirents from a file */
  337. return coda_venus_readdir(coda_file, ctx);
  338. }
  339. static inline unsigned int CDT2DT(unsigned char cdt)
  340. {
  341. unsigned int dt;
  342. switch(cdt) {
  343. case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
  344. case CDT_FIFO: dt = DT_FIFO; break;
  345. case CDT_CHR: dt = DT_CHR; break;
  346. case CDT_DIR: dt = DT_DIR; break;
  347. case CDT_BLK: dt = DT_BLK; break;
  348. case CDT_REG: dt = DT_REG; break;
  349. case CDT_LNK: dt = DT_LNK; break;
  350. case CDT_SOCK: dt = DT_SOCK; break;
  351. case CDT_WHT: dt = DT_WHT; break;
  352. default: dt = DT_UNKNOWN; break;
  353. }
  354. return dt;
  355. }
  356. /* support routines */
  357. static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
  358. {
  359. struct coda_file_info *cfi;
  360. struct coda_inode_info *cii;
  361. struct file *host_file;
  362. struct dentry *de;
  363. struct venus_dirent *vdir;
  364. unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
  365. unsigned int type;
  366. struct qstr name;
  367. ino_t ino;
  368. int ret;
  369. cfi = CODA_FTOC(coda_file);
  370. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  371. host_file = cfi->cfi_container;
  372. de = coda_file->f_path.dentry;
  373. cii = ITOC(de->d_inode);
  374. vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
  375. if (!vdir) return -ENOMEM;
  376. if (!dir_emit_dots(coda_file, ctx))
  377. goto out;
  378. while (1) {
  379. /* read entries from the directory file */
  380. ret = kernel_read(host_file, ctx->pos - 2, (char *)vdir,
  381. sizeof(*vdir));
  382. if (ret < 0) {
  383. printk(KERN_ERR "coda readdir: read dir %s failed %d\n",
  384. coda_f2s(&cii->c_fid), ret);
  385. break;
  386. }
  387. if (ret == 0) break; /* end of directory file reached */
  388. /* catch truncated reads */
  389. if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
  390. printk(KERN_ERR "coda readdir: short read on %s\n",
  391. coda_f2s(&cii->c_fid));
  392. ret = -EBADF;
  393. break;
  394. }
  395. /* validate whether the directory file actually makes sense */
  396. if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
  397. printk(KERN_ERR "coda readdir: invalid dir %s\n",
  398. coda_f2s(&cii->c_fid));
  399. ret = -EBADF;
  400. break;
  401. }
  402. name.len = vdir->d_namlen;
  403. name.name = vdir->d_name;
  404. /* Make sure we skip '.' and '..', we already got those */
  405. if (name.name[0] == '.' && (name.len == 1 ||
  406. (name.name[1] == '.' && name.len == 2)))
  407. vdir->d_fileno = name.len = 0;
  408. /* skip null entries */
  409. if (vdir->d_fileno && name.len) {
  410. ino = vdir->d_fileno;
  411. type = CDT2DT(vdir->d_type);
  412. if (!dir_emit(ctx, name.name, name.len, ino, type))
  413. break;
  414. }
  415. /* we'll always have progress because d_reclen is unsigned and
  416. * we've already established it is non-zero. */
  417. ctx->pos += vdir->d_reclen;
  418. }
  419. out:
  420. kfree(vdir);
  421. return 0;
  422. }
  423. /* called when a cache lookup succeeds */
  424. static int coda_dentry_revalidate(struct dentry *de, unsigned int flags)
  425. {
  426. struct inode *inode;
  427. struct coda_inode_info *cii;
  428. if (flags & LOOKUP_RCU)
  429. return -ECHILD;
  430. inode = de->d_inode;
  431. if (!inode || coda_isroot(inode))
  432. goto out;
  433. if (is_bad_inode(inode))
  434. goto bad;
  435. cii = ITOC(de->d_inode);
  436. if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
  437. goto out;
  438. shrink_dcache_parent(de);
  439. /* propagate for a flush */
  440. if (cii->c_flags & C_FLUSH)
  441. coda_flag_inode_children(inode, C_FLUSH);
  442. if (d_count(de) > 1)
  443. /* pretend it's valid, but don't change the flags */
  444. goto out;
  445. /* clear the flags. */
  446. spin_lock(&cii->c_lock);
  447. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  448. spin_unlock(&cii->c_lock);
  449. bad:
  450. return 0;
  451. out:
  452. return 1;
  453. }
  454. /*
  455. * This is the callback from dput() when d_count is going to 0.
  456. * We use this to unhash dentries with bad inodes.
  457. */
  458. static int coda_dentry_delete(const struct dentry * dentry)
  459. {
  460. int flags;
  461. if (!dentry->d_inode)
  462. return 0;
  463. flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
  464. if (is_bad_inode(dentry->d_inode) || flags) {
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. /*
  470. * This is called when we want to check if the inode has
  471. * changed on the server. Coda makes this easy since the
  472. * cache manager Venus issues a downcall to the kernel when this
  473. * happens
  474. */
  475. int coda_revalidate_inode(struct dentry *dentry)
  476. {
  477. struct coda_vattr attr;
  478. int error;
  479. int old_mode;
  480. ino_t old_ino;
  481. struct inode *inode = dentry->d_inode;
  482. struct coda_inode_info *cii = ITOC(inode);
  483. if (!cii->c_flags)
  484. return 0;
  485. if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
  486. error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
  487. if (error)
  488. return -EIO;
  489. /* this inode may be lost if:
  490. - it's ino changed
  491. - type changes must be permitted for repair and
  492. missing mount points.
  493. */
  494. old_mode = inode->i_mode;
  495. old_ino = inode->i_ino;
  496. coda_vattr_to_iattr(inode, &attr);
  497. if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
  498. printk("Coda: inode %ld, fid %s changed type!\n",
  499. inode->i_ino, coda_f2s(&(cii->c_fid)));
  500. }
  501. /* the following can happen when a local fid is replaced
  502. with a global one, here we lose and declare the inode bad */
  503. if (inode->i_ino != old_ino)
  504. return -EIO;
  505. coda_flag_inode_children(inode, C_FLUSH);
  506. spin_lock(&cii->c_lock);
  507. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  508. spin_unlock(&cii->c_lock);
  509. }
  510. return 0;
  511. }