vxfs_lookup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * @retp: return buffer
  156. * @filler: filldir callback
  157. *
  158. * Description:
  159. * vxfs_readdir fills @retp with directory entries from @fp
  160. * using the VFS supplied callback @filler.
  161. *
  162. * Returns:
  163. * Zero.
  164. */
  165. static int
  166. vxfs_readdir(struct file *fp, struct dir_context *ctx)
  167. {
  168. struct inode *ip = file_inode(fp);
  169. struct super_block *sbp = ip->i_sb;
  170. u_long bsize = sbp->s_blocksize;
  171. loff_t pos, limit;
  172. struct vxfs_sb_info *sbi = VXFS_SBI(sbp);
  173. if (ctx->pos == 0) {
  174. if (!dir_emit_dot(fp, ctx))
  175. goto out;
  176. ctx->pos++;
  177. }
  178. if (ctx->pos == 1) {
  179. if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR))
  180. goto out;
  181. ctx->pos++;
  182. }
  183. limit = VXFS_DIRROUND(ip->i_size);
  184. if (ctx->pos > limit)
  185. goto out;
  186. pos = ctx->pos & ~3L;
  187. while (pos < limit) {
  188. struct page *pp;
  189. char *kaddr;
  190. int pg_ofs = pos & ~PAGE_MASK;
  191. int rc = 0;
  192. pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
  193. if (IS_ERR(pp))
  194. return -ENOMEM;
  195. kaddr = (char *)page_address(pp);
  196. while (pg_ofs < PAGE_SIZE && pos < limit) {
  197. struct vxfs_direct *de;
  198. if ((pos & (bsize - 1)) < 4) {
  199. struct vxfs_dirblk *dbp =
  200. (struct vxfs_dirblk *)
  201. (kaddr + (pos & ~PAGE_MASK));
  202. int overhead = VXFS_DIRBLKOV(sbi, dbp);
  203. pos += overhead;
  204. pg_ofs += overhead;
  205. }
  206. de = (struct vxfs_direct *)(kaddr + pg_ofs);
  207. if (!de->d_reclen) {
  208. pos += bsize - 1;
  209. pos &= ~(bsize - 1);
  210. break;
  211. }
  212. pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
  213. pos += fs16_to_cpu(sbi, de->d_reclen);
  214. if (!de->d_ino)
  215. continue;
  216. rc = dir_emit(ctx, de->d_name,
  217. fs16_to_cpu(sbi, de->d_namelen),
  218. fs32_to_cpu(sbi, de->d_ino),
  219. DT_UNKNOWN);
  220. if (!rc) {
  221. /* the dir entry was not read, fix pos. */
  222. pos -= fs16_to_cpu(sbi, de->d_reclen);
  223. break;
  224. }
  225. }
  226. vxfs_put_page(pp);
  227. if (!rc)
  228. break;
  229. }
  230. ctx->pos = pos | 2;
  231. out:
  232. return 0;
  233. }