dir.c 14 KB

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