vxfs_lookup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2000-2001 Christoph Hellwig.
  4. * Copyright (c) 2016 Krzysztof Blaszkowski
  5. */
  6. /*
  7. * Veritas filesystem driver - lookup and other directory related code.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/time.h>
  11. #include <linux/mm.h>
  12. #include <linux/highmem.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pagemap.h>
  15. #include "vxfs.h"
  16. #include "vxfs_dir.h"
  17. #include "vxfs_inode.h"
  18. #include "vxfs_extern.h"
  19. /*
  20. * Number of VxFS blocks per page.
  21. */
  22. #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize))
  23. static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int);
  24. static int vxfs_readdir(struct file *, struct dir_context *);
  25. const struct inode_operations vxfs_dir_inode_ops = {
  26. .lookup = vxfs_lookup,
  27. };
  28. const struct file_operations vxfs_dir_operations = {
  29. .llseek = generic_file_llseek,
  30. .read = generic_read_dir,
  31. .iterate_shared = vxfs_readdir,
  32. };
  33. /**
  34. * vxfs_find_entry - find a mathing directory entry for a dentry
  35. * @ip: directory inode
  36. * @dp: dentry for which we want to find a direct
  37. * @ppp: gets filled with the page the return value sits in
  38. *
  39. * Description:
  40. * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory
  41. * cache entry @dp. @ppp will be filled with the page the return
  42. * value resides in.
  43. *
  44. * Returns:
  45. * The wanted direct on success, else a NULL pointer.
  46. */
  47. static struct vxfs_direct *
  48. vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp)
  49. {
  50. u_long bsize = ip->i_sb->s_blocksize;
  51. const char *name = dp->d_name.name;
  52. int namelen = dp->d_name.len;
  53. loff_t limit = VXFS_DIRROUND(ip->i_size);
  54. struct vxfs_direct *de_exit = NULL;
  55. loff_t pos = 0;
  56. struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb);
  57. while (pos < limit) {
  58. struct page *pp;
  59. char *kaddr;
  60. int pg_ofs = pos & ~PAGE_MASK;
  61. pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
  62. if (IS_ERR(pp))
  63. return NULL;
  64. kaddr = (char *)page_address(pp);
  65. while (pg_ofs < PAGE_SIZE && pos < limit) {
  66. struct vxfs_direct *de;
  67. if ((pos & (bsize - 1)) < 4) {
  68. struct vxfs_dirblk *dbp =
  69. (struct vxfs_dirblk *)
  70. (kaddr + (pos & ~PAGE_MASK));
  71. int overhead = VXFS_DIRBLKOV(sbi, dbp);
  72. pos += overhead;
  73. pg_ofs += overhead;
  74. }
  75. de = (struct vxfs_direct *)(kaddr + pg_ofs);
  76. if (!de->d_reclen) {
  77. pos += bsize - 1;
  78. pos &= ~(bsize - 1);
  79. break;
  80. }
  81. pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
  82. pos += fs16_to_cpu(sbi, de->d_reclen);
  83. if (!de->d_ino)
  84. continue;
  85. if (namelen != fs16_to_cpu(sbi, de->d_namelen))
  86. continue;
  87. if (!memcmp(name, de->d_name, namelen)) {
  88. *ppp = pp;
  89. de_exit = de;
  90. break;
  91. }
  92. }
  93. if (!de_exit)
  94. vxfs_put_page(pp);
  95. else
  96. break;
  97. }
  98. return de_exit;
  99. }
  100. /**
  101. * vxfs_inode_by_name - find inode number for dentry
  102. * @dip: directory to search in
  103. * @dp: dentry we search for
  104. *
  105. * Description:
  106. * vxfs_inode_by_name finds out the inode number of
  107. * the path component described by @dp in @dip.
  108. *
  109. * Returns:
  110. * The wanted inode number on success, else Zero.
  111. */
  112. static ino_t
  113. vxfs_inode_by_name(struct inode *dip, struct dentry *dp)
  114. {
  115. struct vxfs_direct *de;
  116. struct page *pp;
  117. ino_t ino = 0;
  118. de = vxfs_find_entry(dip, dp, &pp);
  119. if (de) {
  120. ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino);
  121. kunmap(pp);
  122. put_page(pp);
  123. }
  124. return (ino);
  125. }
  126. /**
  127. * vxfs_lookup - lookup pathname component
  128. * @dip: dir in which we lookup
  129. * @dp: dentry we lookup
  130. * @flags: lookup flags
  131. *
  132. * Description:
  133. * vxfs_lookup tries to lookup the pathname component described
  134. * by @dp in @dip.
  135. *
  136. * Returns:
  137. * A NULL-pointer on success, else a negative error code encoded
  138. * in the return pointer.
  139. */
  140. static struct dentry *
  141. vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags)
  142. {
  143. struct inode *ip = NULL;
  144. ino_t ino;
  145. if (dp->d_name.len > VXFS_NAMELEN)
  146. return ERR_PTR(-ENAMETOOLONG);
  147. ino = vxfs_inode_by_name(dip, dp);
  148. if (ino)
  149. ip = vxfs_iget(dip->i_sb, ino);
  150. return d_splice_alias(ip, dp);
  151. }
  152. /**
  153. * vxfs_readdir - read a directory
  154. * @fp: the directory to read
  155. * @ctx: dir_context for filldir/readdir
  156. *
  157. * Description:
  158. * vxfs_readdir fills @retp with directory entries from @fp
  159. * using the VFS supplied callback @filler.
  160. *
  161. * Returns:
  162. * Zero.
  163. */
  164. static int
  165. vxfs_readdir(struct file *fp, struct dir_context *ctx)
  166. {
  167. struct inode *ip = file_inode(fp);
  168. struct super_block *sbp = ip->i_sb;
  169. u_long bsize = sbp->s_blocksize;
  170. loff_t pos, limit;
  171. struct vxfs_sb_info *sbi = VXFS_SBI(sbp);
  172. if (ctx->pos == 0) {
  173. if (!dir_emit_dot(fp, ctx))
  174. goto out;
  175. ctx->pos++;
  176. }
  177. if (ctx->pos == 1) {
  178. if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR))
  179. goto out;
  180. ctx->pos++;
  181. }
  182. limit = VXFS_DIRROUND(ip->i_size);
  183. if (ctx->pos > limit)
  184. goto out;
  185. pos = ctx->pos & ~3L;
  186. while (pos < limit) {
  187. struct page *pp;
  188. char *kaddr;
  189. int pg_ofs = pos & ~PAGE_MASK;
  190. int rc = 0;
  191. pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
  192. if (IS_ERR(pp))
  193. return -ENOMEM;
  194. kaddr = (char *)page_address(pp);
  195. while (pg_ofs < PAGE_SIZE && pos < limit) {
  196. struct vxfs_direct *de;
  197. if ((pos & (bsize - 1)) < 4) {
  198. struct vxfs_dirblk *dbp =
  199. (struct vxfs_dirblk *)
  200. (kaddr + (pos & ~PAGE_MASK));
  201. int overhead = VXFS_DIRBLKOV(sbi, dbp);
  202. pos += overhead;
  203. pg_ofs += overhead;
  204. }
  205. de = (struct vxfs_direct *)(kaddr + pg_ofs);
  206. if (!de->d_reclen) {
  207. pos += bsize - 1;
  208. pos &= ~(bsize - 1);
  209. break;
  210. }
  211. pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
  212. pos += fs16_to_cpu(sbi, de->d_reclen);
  213. if (!de->d_ino)
  214. continue;
  215. rc = dir_emit(ctx, de->d_name,
  216. fs16_to_cpu(sbi, de->d_namelen),
  217. fs32_to_cpu(sbi, de->d_ino),
  218. DT_UNKNOWN);
  219. if (!rc) {
  220. /* the dir entry was not read, fix pos. */
  221. pos -= fs16_to_cpu(sbi, de->d_reclen);
  222. break;
  223. }
  224. }
  225. vxfs_put_page(pp);
  226. if (!rc)
  227. break;
  228. }
  229. ctx->pos = pos | 2;
  230. out:
  231. return 0;
  232. }