dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 (coda@cs.cmu.edu).
  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 <linux/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 (%*s)\n",
  44. coda_i2s(dir), (int)length, name);
  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 inode *inode, int mask)
  64. {
  65. int error;
  66. if (mask & MAY_NOT_BLOCK)
  67. return -ECHILD;
  68. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  69. if (!mask)
  70. return 0;
  71. if ((mask & MAY_EXEC) && !execute_ok(inode))
  72. return -EACCES;
  73. if (coda_cache_check(inode, mask))
  74. return 0;
  75. error = venus_access(inode->i_sb, coda_i2f(inode), mask);
  76. if (!error)
  77. coda_cache_enter(inode, mask);
  78. return error;
  79. }
  80. static inline void coda_dir_update_mtime(struct inode *dir)
  81. {
  82. #ifdef REQUERY_VENUS_FOR_MTIME
  83. /* invalidate the directory cnode's attributes so we refetch the
  84. * attributes from venus next time the inode is referenced */
  85. coda_flag_inode(dir, C_VATTR);
  86. #else
  87. /* optimistically we can also act as if our nose bleeds. The
  88. * granularity of the mtime is coarse anyways so we might actually be
  89. * right most of the time. Note: we only do this for directories. */
  90. dir->i_mtime = dir->i_ctime = current_time(dir);
  91. #endif
  92. }
  93. /* we have to wrap inc_nlink/drop_nlink because sometimes userspace uses a
  94. * trick to fool GNU find's optimizations. If we can't be sure of the link
  95. * (because of volume mount points) we set i_nlink to 1 which forces find
  96. * to consider every child as a possible directory. We should also never
  97. * see an increment or decrement for deleted directories where i_nlink == 0 */
  98. static inline void coda_dir_inc_nlink(struct inode *dir)
  99. {
  100. if (dir->i_nlink >= 2)
  101. inc_nlink(dir);
  102. }
  103. static inline void coda_dir_drop_nlink(struct inode *dir)
  104. {
  105. if (dir->i_nlink > 2)
  106. drop_nlink(dir);
  107. }
  108. /* creation routines: create, mknod, mkdir, link, symlink */
  109. static int coda_create(struct inode *dir, struct dentry *de, umode_t mode, bool excl)
  110. {
  111. int error;
  112. const char *name=de->d_name.name;
  113. int length=de->d_name.len;
  114. struct inode *inode;
  115. struct CodaFid newfid;
  116. struct coda_vattr attrs;
  117. if (is_root_inode(dir) && coda_iscontrol(name, length))
  118. return -EPERM;
  119. error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
  120. 0, mode, &newfid, &attrs);
  121. if (error)
  122. goto err_out;
  123. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  124. if (IS_ERR(inode)) {
  125. error = PTR_ERR(inode);
  126. goto err_out;
  127. }
  128. /* invalidate the directory cnode's attributes */
  129. coda_dir_update_mtime(dir);
  130. d_instantiate(de, inode);
  131. return 0;
  132. err_out:
  133. d_drop(de);
  134. return error;
  135. }
  136. static int coda_mkdir(struct inode *dir, struct dentry *de, umode_t mode)
  137. {
  138. struct inode *inode;
  139. struct coda_vattr attrs;
  140. const char *name = de->d_name.name;
  141. int len = de->d_name.len;
  142. int error;
  143. struct CodaFid newfid;
  144. if (is_root_inode(dir) && coda_iscontrol(name, len))
  145. return -EPERM;
  146. attrs.va_mode = mode;
  147. error = venus_mkdir(dir->i_sb, coda_i2f(dir),
  148. name, len, &newfid, &attrs);
  149. if (error)
  150. goto err_out;
  151. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  152. if (IS_ERR(inode)) {
  153. error = PTR_ERR(inode);
  154. goto err_out;
  155. }
  156. /* invalidate the directory cnode's attributes */
  157. coda_dir_inc_nlink(dir);
  158. coda_dir_update_mtime(dir);
  159. d_instantiate(de, inode);
  160. return 0;
  161. err_out:
  162. d_drop(de);
  163. return error;
  164. }
  165. /* try to make de an entry in dir_inodde linked to source_de */
  166. static int coda_link(struct dentry *source_de, struct inode *dir_inode,
  167. struct dentry *de)
  168. {
  169. struct inode *inode = d_inode(source_de);
  170. const char * name = de->d_name.name;
  171. int len = de->d_name.len;
  172. int error;
  173. if (is_root_inode(dir_inode) && coda_iscontrol(name, len))
  174. return -EPERM;
  175. error = venus_link(dir_inode->i_sb, coda_i2f(inode),
  176. coda_i2f(dir_inode), (const char *)name, len);
  177. if (error) {
  178. d_drop(de);
  179. return error;
  180. }
  181. coda_dir_update_mtime(dir_inode);
  182. ihold(inode);
  183. d_instantiate(de, inode);
  184. inc_nlink(inode);
  185. return 0;
  186. }
  187. static int coda_symlink(struct inode *dir_inode, struct dentry *de,
  188. const char *symname)
  189. {
  190. const char *name = de->d_name.name;
  191. int len = de->d_name.len;
  192. int symlen;
  193. int error;
  194. if (is_root_inode(dir_inode) && coda_iscontrol(name, len))
  195. return -EPERM;
  196. symlen = strlen(symname);
  197. if (symlen > CODA_MAXPATHLEN)
  198. return -ENAMETOOLONG;
  199. /*
  200. * This entry is now negative. Since we do not create
  201. * an inode for the entry we have to drop it.
  202. */
  203. d_drop(de);
  204. error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
  205. symname, symlen);
  206. /* mtime is no good anymore */
  207. if (!error)
  208. coda_dir_update_mtime(dir_inode);
  209. return error;
  210. }
  211. /* destruction routines: unlink, rmdir */
  212. static int coda_unlink(struct inode *dir, struct dentry *de)
  213. {
  214. int error;
  215. const char *name = de->d_name.name;
  216. int len = de->d_name.len;
  217. error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
  218. if (error)
  219. return error;
  220. coda_dir_update_mtime(dir);
  221. drop_nlink(d_inode(de));
  222. return 0;
  223. }
  224. static int coda_rmdir(struct inode *dir, struct dentry *de)
  225. {
  226. const char *name = de->d_name.name;
  227. int len = de->d_name.len;
  228. int error;
  229. error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
  230. if (!error) {
  231. /* VFS may delete the child */
  232. if (d_really_is_positive(de))
  233. clear_nlink(d_inode(de));
  234. /* fix the link count of the parent */
  235. coda_dir_drop_nlink(dir);
  236. coda_dir_update_mtime(dir);
  237. }
  238. return error;
  239. }
  240. /* rename */
  241. static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
  242. struct inode *new_dir, struct dentry *new_dentry,
  243. unsigned int flags)
  244. {
  245. const char *old_name = old_dentry->d_name.name;
  246. const char *new_name = new_dentry->d_name.name;
  247. int old_length = old_dentry->d_name.len;
  248. int new_length = new_dentry->d_name.len;
  249. int error;
  250. if (flags)
  251. return -EINVAL;
  252. error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
  253. coda_i2f(new_dir), old_length, new_length,
  254. (const char *) old_name, (const char *)new_name);
  255. if (!error) {
  256. if (d_really_is_positive(new_dentry)) {
  257. if (d_is_dir(new_dentry)) {
  258. coda_dir_drop_nlink(old_dir);
  259. coda_dir_inc_nlink(new_dir);
  260. }
  261. coda_dir_update_mtime(old_dir);
  262. coda_dir_update_mtime(new_dir);
  263. coda_flag_inode(d_inode(new_dentry), C_VATTR);
  264. } else {
  265. coda_flag_inode(old_dir, C_VATTR);
  266. coda_flag_inode(new_dir, C_VATTR);
  267. }
  268. }
  269. return error;
  270. }
  271. static inline unsigned int CDT2DT(unsigned char cdt)
  272. {
  273. unsigned int dt;
  274. switch(cdt) {
  275. case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
  276. case CDT_FIFO: dt = DT_FIFO; break;
  277. case CDT_CHR: dt = DT_CHR; break;
  278. case CDT_DIR: dt = DT_DIR; break;
  279. case CDT_BLK: dt = DT_BLK; break;
  280. case CDT_REG: dt = DT_REG; break;
  281. case CDT_LNK: dt = DT_LNK; break;
  282. case CDT_SOCK: dt = DT_SOCK; break;
  283. case CDT_WHT: dt = DT_WHT; break;
  284. default: dt = DT_UNKNOWN; break;
  285. }
  286. return dt;
  287. }
  288. /* support routines */
  289. static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
  290. {
  291. struct coda_file_info *cfi;
  292. struct coda_inode_info *cii;
  293. struct file *host_file;
  294. struct venus_dirent *vdir;
  295. unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
  296. unsigned int type;
  297. struct qstr name;
  298. ino_t ino;
  299. int ret;
  300. cfi = CODA_FTOC(coda_file);
  301. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  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. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  361. host_file = cfi->cfi_container;
  362. if (host_file->f_op->iterate || host_file->f_op->iterate_shared) {
  363. struct inode *host_inode = file_inode(host_file);
  364. ret = -ENOENT;
  365. if (!IS_DEADDIR(host_inode)) {
  366. if (host_file->f_op->iterate_shared) {
  367. inode_lock_shared(host_inode);
  368. ret = host_file->f_op->iterate_shared(host_file, ctx);
  369. file_accessed(host_file);
  370. inode_unlock_shared(host_inode);
  371. } else {
  372. inode_lock(host_inode);
  373. ret = host_file->f_op->iterate(host_file, ctx);
  374. file_accessed(host_file);
  375. inode_unlock(host_inode);
  376. }
  377. }
  378. return ret;
  379. }
  380. /* Venus: we must read Venus dirents from a file */
  381. return coda_venus_readdir(coda_file, ctx);
  382. }
  383. /* called when a cache lookup succeeds */
  384. static int coda_dentry_revalidate(struct dentry *de, unsigned int flags)
  385. {
  386. struct inode *inode;
  387. struct coda_inode_info *cii;
  388. if (flags & LOOKUP_RCU)
  389. return -ECHILD;
  390. inode = d_inode(de);
  391. if (!inode || is_root_inode(inode))
  392. goto out;
  393. if (is_bad_inode(inode))
  394. goto bad;
  395. cii = ITOC(d_inode(de));
  396. if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
  397. goto out;
  398. shrink_dcache_parent(de);
  399. /* propagate for a flush */
  400. if (cii->c_flags & C_FLUSH)
  401. coda_flag_inode_children(inode, C_FLUSH);
  402. if (d_count(de) > 1)
  403. /* pretend it's valid, but don't change the flags */
  404. goto out;
  405. /* clear the flags. */
  406. spin_lock(&cii->c_lock);
  407. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  408. spin_unlock(&cii->c_lock);
  409. bad:
  410. return 0;
  411. out:
  412. return 1;
  413. }
  414. /*
  415. * This is the callback from dput() when d_count is going to 0.
  416. * We use this to unhash dentries with bad inodes.
  417. */
  418. static int coda_dentry_delete(const struct dentry * dentry)
  419. {
  420. int flags;
  421. if (d_really_is_negative(dentry))
  422. return 0;
  423. flags = (ITOC(d_inode(dentry))->c_flags) & C_PURGE;
  424. if (is_bad_inode(d_inode(dentry)) || flags) {
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. /*
  430. * This is called when we want to check if the inode has
  431. * changed on the server. Coda makes this easy since the
  432. * cache manager Venus issues a downcall to the kernel when this
  433. * happens
  434. */
  435. int coda_revalidate_inode(struct inode *inode)
  436. {
  437. struct coda_vattr attr;
  438. int error;
  439. int old_mode;
  440. ino_t old_ino;
  441. struct coda_inode_info *cii = ITOC(inode);
  442. if (!cii->c_flags)
  443. return 0;
  444. if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
  445. error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
  446. if (error)
  447. return -EIO;
  448. /* this inode may be lost if:
  449. - it's ino changed
  450. - type changes must be permitted for repair and
  451. missing mount points.
  452. */
  453. old_mode = inode->i_mode;
  454. old_ino = inode->i_ino;
  455. coda_vattr_to_iattr(inode, &attr);
  456. if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
  457. pr_warn("inode %ld, fid %s changed type!\n",
  458. inode->i_ino, coda_f2s(&(cii->c_fid)));
  459. }
  460. /* the following can happen when a local fid is replaced
  461. with a global one, here we lose and declare the inode bad */
  462. if (inode->i_ino != old_ino)
  463. return -EIO;
  464. coda_flag_inode_children(inode, C_FLUSH);
  465. spin_lock(&cii->c_lock);
  466. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  467. spin_unlock(&cii->c_lock);
  468. }
  469. return 0;
  470. }
  471. const struct dentry_operations coda_dentry_operations = {
  472. .d_revalidate = coda_dentry_revalidate,
  473. .d_delete = coda_dentry_delete,
  474. };
  475. const struct inode_operations coda_dir_inode_operations = {
  476. .create = coda_create,
  477. .lookup = coda_lookup,
  478. .link = coda_link,
  479. .unlink = coda_unlink,
  480. .symlink = coda_symlink,
  481. .mkdir = coda_mkdir,
  482. .rmdir = coda_rmdir,
  483. .mknod = CODA_EIO_ERROR,
  484. .rename = coda_rename,
  485. .permission = coda_permission,
  486. .getattr = coda_getattr,
  487. .setattr = coda_setattr,
  488. };
  489. const struct file_operations coda_dir_operations = {
  490. .llseek = generic_file_llseek,
  491. .read = generic_read_dir,
  492. .iterate = coda_readdir,
  493. .open = coda_open,
  494. .release = coda_release,
  495. .fsync = coda_fsync,
  496. };