vxfs_lookup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2000-2001 Christoph Hellwig.
  3. * Copyright (c) 2016 Krzysztof Blaszkowski
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * Alternatively, this software may be distributed under the terms of the
  16. * GNU General Public License ("GPL").
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /*
  31. * Veritas filesystem driver - lookup and other directory related code.
  32. */
  33. #include <linux/fs.h>
  34. #include <linux/time.h>
  35. #include <linux/mm.h>
  36. #include <linux/highmem.h>
  37. #include <linux/kernel.h>
  38. #include <linux/pagemap.h>
  39. #include "vxfs.h"
  40. #include "vxfs_dir.h"
  41. #include "vxfs_inode.h"
  42. #include "vxfs_extern.h"
  43. /*
  44. * Number of VxFS blocks per page.
  45. */
  46. #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize))
  47. static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int);
  48. static int vxfs_readdir(struct file *, struct dir_context *);
  49. const struct inode_operations vxfs_dir_inode_ops = {
  50. .lookup = vxfs_lookup,
  51. };
  52. const struct file_operations vxfs_dir_operations = {
  53. .llseek = generic_file_llseek,
  54. .read = generic_read_dir,
  55. .iterate_shared = vxfs_readdir,
  56. };
  57. /**
  58. * vxfs_find_entry - find a mathing directory entry for a dentry
  59. * @ip: directory inode
  60. * @dp: dentry for which we want to find a direct
  61. * @ppp: gets filled with the page the return value sits in
  62. *
  63. * Description:
  64. * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory
  65. * cache entry @dp. @ppp will be filled with the page the return
  66. * value resides in.
  67. *
  68. * Returns:
  69. * The wanted direct on success, else a NULL pointer.
  70. */
  71. static struct vxfs_direct *
  72. vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp)
  73. {
  74. u_long bsize = ip->i_sb->s_blocksize;
  75. const char *name = dp->d_name.name;
  76. int namelen = dp->d_name.len;
  77. loff_t limit = VXFS_DIRROUND(ip->i_size);
  78. struct vxfs_direct *de_exit = NULL;
  79. loff_t pos = 0;
  80. struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb);
  81. while (pos < limit) {
  82. struct page *pp;
  83. char *kaddr;
  84. int pg_ofs = pos & ~PAGE_MASK;
  85. pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
  86. if (IS_ERR(pp))
  87. return NULL;
  88. kaddr = (char *)page_address(pp);
  89. while (pg_ofs < PAGE_SIZE && pos < limit) {
  90. struct vxfs_direct *de;
  91. if ((pos & (bsize - 1)) < 4) {
  92. struct vxfs_dirblk *dbp =
  93. (struct vxfs_dirblk *)
  94. (kaddr + (pos & ~PAGE_MASK));
  95. int overhead = VXFS_DIRBLKOV(sbi, dbp);
  96. pos += overhead;
  97. pg_ofs += overhead;
  98. }
  99. de = (struct vxfs_direct *)(kaddr + pg_ofs);
  100. if (!de->d_reclen) {
  101. pos += bsize - 1;
  102. pos &= ~(bsize - 1);
  103. break;
  104. }
  105. pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
  106. pos += fs16_to_cpu(sbi, de->d_reclen);
  107. if (!de->d_ino)
  108. continue;
  109. if (namelen != fs16_to_cpu(sbi, de->d_namelen))
  110. continue;
  111. if (!memcmp(name, de->d_name, namelen)) {
  112. *ppp = pp;
  113. de_exit = de;
  114. break;
  115. }
  116. }
  117. if (!de_exit)
  118. vxfs_put_page(pp);
  119. else
  120. break;
  121. }
  122. return de_exit;
  123. }
  124. /**
  125. * vxfs_inode_by_name - find inode number for dentry
  126. * @dip: directory to search in
  127. * @dp: dentry we search for
  128. *
  129. * Description:
  130. * vxfs_inode_by_name finds out the inode number of
  131. * the path component described by @dp in @dip.
  132. *
  133. * Returns:
  134. * The wanted inode number on success, else Zero.
  135. */
  136. static ino_t
  137. vxfs_inode_by_name(struct inode *dip, struct dentry *dp)
  138. {
  139. struct vxfs_direct *de;
  140. struct page *pp;
  141. ino_t ino = 0;
  142. de = vxfs_find_entry(dip, dp, &pp);
  143. if (de) {
  144. ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino);
  145. kunmap(pp);
  146. put_page(pp);
  147. }
  148. return (ino);
  149. }
  150. /**
  151. * vxfs_lookup - lookup pathname component
  152. * @dip: dir in which we lookup
  153. * @dp: dentry we lookup
  154. * @flags: lookup flags
  155. *
  156. * Description:
  157. * vxfs_lookup tries to lookup the pathname component described
  158. * by @dp in @dip.
  159. *
  160. * Returns:
  161. * A NULL-pointer on success, else a negative error code encoded
  162. * in the return pointer.
  163. */
  164. static struct dentry *
  165. vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags)
  166. {
  167. struct inode *ip = NULL;
  168. ino_t ino;
  169. if (dp->d_name.len > VXFS_NAMELEN)
  170. return ERR_PTR(-ENAMETOOLONG);
  171. ino = vxfs_inode_by_name(dip, dp);
  172. if (ino)
  173. ip = vxfs_iget(dip->i_sb, ino);
  174. return d_splice_alias(ip, dp);
  175. }
  176. /**
  177. * vxfs_readdir - read a directory
  178. * @fp: the directory to read
  179. * @retp: return buffer
  180. * @filler: filldir callback
  181. *
  182. * Description:
  183. * vxfs_readdir fills @retp with directory entries from @fp
  184. * using the VFS supplied callback @filler.
  185. *
  186. * Returns:
  187. * Zero.
  188. */
  189. static int
  190. vxfs_readdir(struct file *fp, struct dir_context *ctx)
  191. {
  192. struct inode *ip = file_inode(fp);
  193. struct super_block *sbp = ip->i_sb;
  194. u_long bsize = sbp->s_blocksize;
  195. loff_t pos, limit;
  196. struct vxfs_sb_info *sbi = VXFS_SBI(sbp);
  197. if (ctx->pos == 0) {
  198. if (!dir_emit_dot(fp, ctx))
  199. goto out;
  200. ctx->pos++;
  201. }
  202. if (ctx->pos == 1) {
  203. if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR))
  204. goto out;
  205. ctx->pos++;
  206. }
  207. limit = VXFS_DIRROUND(ip->i_size);
  208. if (ctx->pos > limit)
  209. goto out;
  210. pos = ctx->pos & ~3L;
  211. while (pos < limit) {
  212. struct page *pp;
  213. char *kaddr;
  214. int pg_ofs = pos & ~PAGE_MASK;
  215. int rc = 0;
  216. pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
  217. if (IS_ERR(pp))
  218. return -ENOMEM;
  219. kaddr = (char *)page_address(pp);
  220. while (pg_ofs < PAGE_SIZE && pos < limit) {
  221. struct vxfs_direct *de;
  222. if ((pos & (bsize - 1)) < 4) {
  223. struct vxfs_dirblk *dbp =
  224. (struct vxfs_dirblk *)
  225. (kaddr + (pos & ~PAGE_MASK));
  226. int overhead = VXFS_DIRBLKOV(sbi, dbp);
  227. pos += overhead;
  228. pg_ofs += overhead;
  229. }
  230. de = (struct vxfs_direct *)(kaddr + pg_ofs);
  231. if (!de->d_reclen) {
  232. pos += bsize - 1;
  233. pos &= ~(bsize - 1);
  234. break;
  235. }
  236. pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
  237. pos += fs16_to_cpu(sbi, de->d_reclen);
  238. if (!de->d_ino)
  239. continue;
  240. rc = dir_emit(ctx, de->d_name,
  241. fs16_to_cpu(sbi, de->d_namelen),
  242. fs32_to_cpu(sbi, de->d_ino),
  243. DT_UNKNOWN);
  244. if (!rc) {
  245. /* the dir entry was not read, fix pos. */
  246. pos -= fs16_to_cpu(sbi, de->d_reclen);
  247. break;
  248. }
  249. }
  250. vxfs_put_page(pp);
  251. if (!rc)
  252. break;
  253. }
  254. ctx->pos = pos | 2;
  255. out:
  256. return 0;
  257. }