dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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(dir);
  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. unsigned int flags)
  243. {
  244. const char *old_name = old_dentry->d_name.name;
  245. const char *new_name = new_dentry->d_name.name;
  246. int old_length = old_dentry->d_name.len;
  247. int new_length = new_dentry->d_name.len;
  248. int error;
  249. if (flags)
  250. return -EINVAL;
  251. error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
  252. coda_i2f(new_dir), old_length, new_length,
  253. (const char *) old_name, (const char *)new_name);
  254. if (!error) {
  255. if (d_really_is_positive(new_dentry)) {
  256. if (d_is_dir(new_dentry)) {
  257. coda_dir_drop_nlink(old_dir);
  258. coda_dir_inc_nlink(new_dir);
  259. }
  260. coda_dir_update_mtime(old_dir);
  261. coda_dir_update_mtime(new_dir);
  262. coda_flag_inode(d_inode(new_dentry), C_VATTR);
  263. } else {
  264. coda_flag_inode(old_dir, C_VATTR);
  265. coda_flag_inode(new_dir, C_VATTR);
  266. }
  267. }
  268. return error;
  269. }
  270. static inline unsigned int CDT2DT(unsigned char cdt)
  271. {
  272. unsigned int dt;
  273. switch(cdt) {
  274. case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
  275. case CDT_FIFO: dt = DT_FIFO; break;
  276. case CDT_CHR: dt = DT_CHR; break;
  277. case CDT_DIR: dt = DT_DIR; break;
  278. case CDT_BLK: dt = DT_BLK; break;
  279. case CDT_REG: dt = DT_REG; break;
  280. case CDT_LNK: dt = DT_LNK; break;
  281. case CDT_SOCK: dt = DT_SOCK; break;
  282. case CDT_WHT: dt = DT_WHT; break;
  283. default: dt = DT_UNKNOWN; break;
  284. }
  285. return dt;
  286. }
  287. /* support routines */
  288. static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
  289. {
  290. struct coda_file_info *cfi;
  291. struct coda_inode_info *cii;
  292. struct file *host_file;
  293. struct venus_dirent *vdir;
  294. unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
  295. unsigned int type;
  296. struct qstr name;
  297. ino_t ino;
  298. int ret;
  299. cfi = CODA_FTOC(coda_file);
  300. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  301. host_file = cfi->cfi_container;
  302. cii = ITOC(file_inode(coda_file));
  303. vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
  304. if (!vdir) return -ENOMEM;
  305. if (!dir_emit_dots(coda_file, ctx))
  306. goto out;
  307. while (1) {
  308. /* read entries from the directory file */
  309. ret = kernel_read(host_file, ctx->pos - 2, (char *)vdir,
  310. sizeof(*vdir));
  311. if (ret < 0) {
  312. pr_err("%s: read dir %s failed %d\n",
  313. __func__, coda_f2s(&cii->c_fid), ret);
  314. break;
  315. }
  316. if (ret == 0) break; /* end of directory file reached */
  317. /* catch truncated reads */
  318. if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
  319. pr_err("%s: short read on %s\n",
  320. __func__, coda_f2s(&cii->c_fid));
  321. ret = -EBADF;
  322. break;
  323. }
  324. /* validate whether the directory file actually makes sense */
  325. if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
  326. pr_err("%s: invalid dir %s\n",
  327. __func__, coda_f2s(&cii->c_fid));
  328. ret = -EBADF;
  329. break;
  330. }
  331. name.len = vdir->d_namlen;
  332. name.name = vdir->d_name;
  333. /* Make sure we skip '.' and '..', we already got those */
  334. if (name.name[0] == '.' && (name.len == 1 ||
  335. (name.name[1] == '.' && name.len == 2)))
  336. vdir->d_fileno = name.len = 0;
  337. /* skip null entries */
  338. if (vdir->d_fileno && name.len) {
  339. ino = vdir->d_fileno;
  340. type = CDT2DT(vdir->d_type);
  341. if (!dir_emit(ctx, name.name, name.len, ino, type))
  342. break;
  343. }
  344. /* we'll always have progress because d_reclen is unsigned and
  345. * we've already established it is non-zero. */
  346. ctx->pos += vdir->d_reclen;
  347. }
  348. out:
  349. kfree(vdir);
  350. return 0;
  351. }
  352. /* file operations for directories */
  353. static int coda_readdir(struct file *coda_file, struct dir_context *ctx)
  354. {
  355. struct coda_file_info *cfi;
  356. struct file *host_file;
  357. int ret;
  358. cfi = CODA_FTOC(coda_file);
  359. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  360. host_file = cfi->cfi_container;
  361. if (host_file->f_op->iterate || host_file->f_op->iterate_shared) {
  362. struct inode *host_inode = file_inode(host_file);
  363. ret = -ENOENT;
  364. if (!IS_DEADDIR(host_inode)) {
  365. if (host_file->f_op->iterate_shared) {
  366. inode_lock_shared(host_inode);
  367. ret = host_file->f_op->iterate_shared(host_file, ctx);
  368. file_accessed(host_file);
  369. inode_unlock_shared(host_inode);
  370. } else {
  371. inode_lock(host_inode);
  372. ret = host_file->f_op->iterate(host_file, ctx);
  373. file_accessed(host_file);
  374. inode_unlock(host_inode);
  375. }
  376. }
  377. return ret;
  378. }
  379. /* Venus: we must read Venus dirents from a file */
  380. return coda_venus_readdir(coda_file, ctx);
  381. }
  382. /* called when a cache lookup succeeds */
  383. static int coda_dentry_revalidate(struct dentry *de, unsigned int flags)
  384. {
  385. struct inode *inode;
  386. struct coda_inode_info *cii;
  387. if (flags & LOOKUP_RCU)
  388. return -ECHILD;
  389. inode = d_inode(de);
  390. if (!inode || is_root_inode(inode))
  391. goto out;
  392. if (is_bad_inode(inode))
  393. goto bad;
  394. cii = ITOC(d_inode(de));
  395. if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
  396. goto out;
  397. shrink_dcache_parent(de);
  398. /* propagate for a flush */
  399. if (cii->c_flags & C_FLUSH)
  400. coda_flag_inode_children(inode, C_FLUSH);
  401. if (d_count(de) > 1)
  402. /* pretend it's valid, but don't change the flags */
  403. goto out;
  404. /* clear the flags. */
  405. spin_lock(&cii->c_lock);
  406. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  407. spin_unlock(&cii->c_lock);
  408. bad:
  409. return 0;
  410. out:
  411. return 1;
  412. }
  413. /*
  414. * This is the callback from dput() when d_count is going to 0.
  415. * We use this to unhash dentries with bad inodes.
  416. */
  417. static int coda_dentry_delete(const struct dentry * dentry)
  418. {
  419. int flags;
  420. if (d_really_is_negative(dentry))
  421. return 0;
  422. flags = (ITOC(d_inode(dentry))->c_flags) & C_PURGE;
  423. if (is_bad_inode(d_inode(dentry)) || flags) {
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. /*
  429. * This is called when we want to check if the inode has
  430. * changed on the server. Coda makes this easy since the
  431. * cache manager Venus issues a downcall to the kernel when this
  432. * happens
  433. */
  434. int coda_revalidate_inode(struct inode *inode)
  435. {
  436. struct coda_vattr attr;
  437. int error;
  438. int old_mode;
  439. ino_t old_ino;
  440. struct coda_inode_info *cii = ITOC(inode);
  441. if (!cii->c_flags)
  442. return 0;
  443. if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
  444. error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
  445. if (error)
  446. return -EIO;
  447. /* this inode may be lost if:
  448. - it's ino changed
  449. - type changes must be permitted for repair and
  450. missing mount points.
  451. */
  452. old_mode = inode->i_mode;
  453. old_ino = inode->i_ino;
  454. coda_vattr_to_iattr(inode, &attr);
  455. if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
  456. pr_warn("inode %ld, fid %s changed type!\n",
  457. inode->i_ino, coda_f2s(&(cii->c_fid)));
  458. }
  459. /* the following can happen when a local fid is replaced
  460. with a global one, here we lose and declare the inode bad */
  461. if (inode->i_ino != old_ino)
  462. return -EIO;
  463. coda_flag_inode_children(inode, C_FLUSH);
  464. spin_lock(&cii->c_lock);
  465. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  466. spin_unlock(&cii->c_lock);
  467. }
  468. return 0;
  469. }
  470. const struct dentry_operations coda_dentry_operations = {
  471. .d_revalidate = coda_dentry_revalidate,
  472. .d_delete = coda_dentry_delete,
  473. };
  474. const struct inode_operations coda_dir_inode_operations = {
  475. .create = coda_create,
  476. .lookup = coda_lookup,
  477. .link = coda_link,
  478. .unlink = coda_unlink,
  479. .symlink = coda_symlink,
  480. .mkdir = coda_mkdir,
  481. .rmdir = coda_rmdir,
  482. .mknod = CODA_EIO_ERROR,
  483. .rename = coda_rename,
  484. .permission = coda_permission,
  485. .getattr = coda_getattr,
  486. .setattr = coda_setattr,
  487. };
  488. const struct file_operations coda_dir_operations = {
  489. .llseek = generic_file_llseek,
  490. .read = generic_read_dir,
  491. .iterate = coda_readdir,
  492. .open = coda_open,
  493. .release = coda_release,
  494. .fsync = coda_fsync,
  495. };