dir.c 14 KB

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