namei.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/staging/erofs/namei.c
  4. *
  5. * Copyright (C) 2017-2018 HUAWEI, Inc.
  6. * http://www.huawei.com/
  7. * Created by Gao Xiang <gaoxiang25@huawei.com>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of the Linux
  11. * distribution for more details.
  12. */
  13. #include "internal.h"
  14. #include "xattr.h"
  15. #include <trace/events/erofs.h>
  16. struct erofs_qstr {
  17. const unsigned char *name;
  18. const unsigned char *end;
  19. };
  20. /* based on the end of qn is accurate and it must have the trailing '\0' */
  21. static inline int dirnamecmp(const struct erofs_qstr *qn,
  22. const struct erofs_qstr *qd,
  23. unsigned int *matched)
  24. {
  25. unsigned int i = *matched;
  26. /*
  27. * on-disk error, let's only BUG_ON in the debugging mode.
  28. * otherwise, it will return 1 to just skip the invalid name
  29. * and go on (in consideration of the lookup performance).
  30. */
  31. DBG_BUGON(qd->name > qd->end);
  32. /* qd could not have trailing '\0' */
  33. /* However it is absolutely safe if < qd->end */
  34. while (qd->name + i < qd->end && qd->name[i] != '\0') {
  35. if (qn->name[i] != qd->name[i]) {
  36. *matched = i;
  37. return qn->name[i] > qd->name[i] ? 1 : -1;
  38. }
  39. ++i;
  40. }
  41. *matched = i;
  42. /* See comments in __d_alloc on the terminating NUL character */
  43. return qn->name[i] == '\0' ? 0 : 1;
  44. }
  45. #define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1))
  46. static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
  47. u8 *data,
  48. unsigned int dirblksize,
  49. const int ndirents)
  50. {
  51. int head, back;
  52. unsigned int startprfx, endprfx;
  53. struct erofs_dirent *const de = (struct erofs_dirent *)data;
  54. /* since the 1st dirent has been evaluated previously */
  55. head = 1;
  56. back = ndirents - 1;
  57. startprfx = endprfx = 0;
  58. while (head <= back) {
  59. const int mid = head + (back - head) / 2;
  60. const int nameoff = nameoff_from_disk(de[mid].nameoff,
  61. dirblksize);
  62. unsigned int matched = min(startprfx, endprfx);
  63. struct erofs_qstr dname = {
  64. .name = data + nameoff,
  65. .end = unlikely(mid >= ndirents - 1) ?
  66. data + dirblksize :
  67. data + nameoff_from_disk(de[mid + 1].nameoff,
  68. dirblksize)
  69. };
  70. /* string comparison without already matched prefix */
  71. int ret = dirnamecmp(name, &dname, &matched);
  72. if (unlikely(!ret)) {
  73. return de + mid;
  74. } else if (ret > 0) {
  75. head = mid + 1;
  76. startprfx = matched;
  77. } else {
  78. back = mid - 1;
  79. endprfx = matched;
  80. }
  81. }
  82. return ERR_PTR(-ENOENT);
  83. }
  84. static struct page *find_target_block_classic(struct inode *dir,
  85. struct erofs_qstr *name,
  86. int *_ndirents)
  87. {
  88. unsigned int startprfx, endprfx;
  89. int head, back;
  90. struct address_space *const mapping = dir->i_mapping;
  91. struct page *candidate = ERR_PTR(-ENOENT);
  92. startprfx = endprfx = 0;
  93. head = 0;
  94. back = inode_datablocks(dir) - 1;
  95. while (head <= back) {
  96. const int mid = head + (back - head) / 2;
  97. struct page *page = read_mapping_page(mapping, mid, NULL);
  98. if (!IS_ERR(page)) {
  99. struct erofs_dirent *de = kmap_atomic(page);
  100. const int nameoff = nameoff_from_disk(de->nameoff,
  101. EROFS_BLKSIZ);
  102. const int ndirents = nameoff / sizeof(*de);
  103. int diff;
  104. unsigned int matched;
  105. struct erofs_qstr dname;
  106. if (unlikely(!ndirents)) {
  107. DBG_BUGON(1);
  108. kunmap_atomic(de);
  109. put_page(page);
  110. page = ERR_PTR(-EIO);
  111. goto out;
  112. }
  113. matched = min(startprfx, endprfx);
  114. dname.name = (u8 *)de + nameoff;
  115. if (ndirents == 1)
  116. dname.end = (u8 *)de + EROFS_BLKSIZ;
  117. else
  118. dname.end = (u8 *)de +
  119. nameoff_from_disk(de[1].nameoff,
  120. EROFS_BLKSIZ);
  121. /* string comparison without already matched prefix */
  122. diff = dirnamecmp(name, &dname, &matched);
  123. kunmap_atomic(de);
  124. if (unlikely(!diff)) {
  125. *_ndirents = 0;
  126. goto out;
  127. } else if (diff > 0) {
  128. head = mid + 1;
  129. startprfx = matched;
  130. if (likely(!IS_ERR(candidate)))
  131. put_page(candidate);
  132. candidate = page;
  133. *_ndirents = ndirents;
  134. } else {
  135. put_page(page);
  136. back = mid - 1;
  137. endprfx = matched;
  138. }
  139. continue;
  140. }
  141. out: /* free if the candidate is valid */
  142. if (!IS_ERR(candidate))
  143. put_page(candidate);
  144. return page;
  145. }
  146. return candidate;
  147. }
  148. int erofs_namei(struct inode *dir,
  149. struct qstr *name,
  150. erofs_nid_t *nid, unsigned int *d_type)
  151. {
  152. int ndirents;
  153. struct page *page;
  154. void *data;
  155. struct erofs_dirent *de;
  156. struct erofs_qstr qn;
  157. if (unlikely(!dir->i_size))
  158. return -ENOENT;
  159. qn.name = name->name;
  160. qn.end = name->name + name->len;
  161. ndirents = 0;
  162. page = find_target_block_classic(dir, &qn, &ndirents);
  163. if (unlikely(IS_ERR(page)))
  164. return PTR_ERR(page);
  165. data = kmap_atomic(page);
  166. /* the target page has been mapped */
  167. if (ndirents)
  168. de = find_target_dirent(&qn, data, EROFS_BLKSIZ, ndirents);
  169. else
  170. de = (struct erofs_dirent *)data;
  171. if (likely(!IS_ERR(de))) {
  172. *nid = le64_to_cpu(de->nid);
  173. *d_type = de->file_type;
  174. }
  175. kunmap_atomic(data);
  176. put_page(page);
  177. return PTR_ERR_OR_ZERO(de);
  178. }
  179. /* NOTE: i_mutex is already held by vfs */
  180. static struct dentry *erofs_lookup(struct inode *dir,
  181. struct dentry *dentry, unsigned int flags)
  182. {
  183. int err;
  184. erofs_nid_t nid;
  185. unsigned d_type;
  186. struct inode *inode;
  187. DBG_BUGON(!d_really_is_negative(dentry));
  188. /* dentry must be unhashed in lookup, no need to worry about */
  189. DBG_BUGON(!d_unhashed(dentry));
  190. trace_erofs_lookup(dir, dentry, flags);
  191. /* file name exceeds fs limit */
  192. if (unlikely(dentry->d_name.len > EROFS_NAME_LEN))
  193. return ERR_PTR(-ENAMETOOLONG);
  194. /* false uninitialized warnings on gcc 4.8.x */
  195. err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
  196. if (err == -ENOENT) {
  197. /* negative dentry */
  198. inode = NULL;
  199. goto negative_out;
  200. } else if (unlikely(err))
  201. return ERR_PTR(err);
  202. debugln("%s, %s (nid %llu) found, d_type %u", __func__,
  203. dentry->d_name.name, nid, d_type);
  204. inode = erofs_iget(dir->i_sb, nid, d_type == EROFS_FT_DIR);
  205. if (IS_ERR(inode))
  206. return ERR_CAST(inode);
  207. negative_out:
  208. return d_splice_alias(inode, dentry);
  209. }
  210. const struct inode_operations erofs_dir_iops = {
  211. .lookup = erofs_lookup,
  212. };
  213. const struct inode_operations erofs_dir_xattr_iops = {
  214. .lookup = erofs_lookup,
  215. #ifdef CONFIG_EROFS_FS_XATTR
  216. .listxattr = erofs_listxattr,
  217. #endif
  218. };