namei.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017-2018 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. * Copyright (C) 2022, Alibaba Cloud
  6. */
  7. #include "xattr.h"
  8. #include <trace/events/erofs.h>
  9. struct erofs_qstr {
  10. const unsigned char *name;
  11. const unsigned char *end;
  12. };
  13. /* based on the end of qn is accurate and it must have the trailing '\0' */
  14. static inline int erofs_dirnamecmp(const struct erofs_qstr *qn,
  15. const struct erofs_qstr *qd,
  16. unsigned int *matched)
  17. {
  18. unsigned int i = *matched;
  19. /*
  20. * on-disk error, let's only BUG_ON in the debugging mode.
  21. * otherwise, it will return 1 to just skip the invalid name
  22. * and go on (in consideration of the lookup performance).
  23. */
  24. DBG_BUGON(qd->name > qd->end);
  25. /* qd could not have trailing '\0' */
  26. /* However it is absolutely safe if < qd->end */
  27. while (qd->name + i < qd->end && qd->name[i] != '\0') {
  28. if (qn->name[i] != qd->name[i]) {
  29. *matched = i;
  30. return qn->name[i] > qd->name[i] ? 1 : -1;
  31. }
  32. ++i;
  33. }
  34. *matched = i;
  35. /* See comments in __d_alloc on the terminating NUL character */
  36. return qn->name[i] == '\0' ? 0 : 1;
  37. }
  38. #define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1))
  39. static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
  40. u8 *data,
  41. unsigned int dirblksize,
  42. const int ndirents)
  43. {
  44. int head, back;
  45. unsigned int startprfx, endprfx;
  46. struct erofs_dirent *const de = (struct erofs_dirent *)data;
  47. /* since the 1st dirent has been evaluated previously */
  48. head = 1;
  49. back = ndirents - 1;
  50. startprfx = endprfx = 0;
  51. while (head <= back) {
  52. const int mid = head + (back - head) / 2;
  53. const int nameoff = nameoff_from_disk(de[mid].nameoff,
  54. dirblksize);
  55. unsigned int matched = min(startprfx, endprfx);
  56. struct erofs_qstr dname = {
  57. .name = data + nameoff,
  58. .end = mid >= ndirents - 1 ?
  59. data + dirblksize :
  60. data + nameoff_from_disk(de[mid + 1].nameoff,
  61. dirblksize)
  62. };
  63. /* string comparison without already matched prefix */
  64. int ret = erofs_dirnamecmp(name, &dname, &matched);
  65. if (!ret) {
  66. return de + mid;
  67. } else if (ret > 0) {
  68. head = mid + 1;
  69. startprfx = matched;
  70. } else {
  71. back = mid - 1;
  72. endprfx = matched;
  73. }
  74. }
  75. return ERR_PTR(-ENOENT);
  76. }
  77. static void *erofs_find_target_block(struct erofs_buf *target,
  78. struct inode *dir, struct erofs_qstr *name, int *_ndirents)
  79. {
  80. unsigned int bsz = i_blocksize(dir);
  81. int head = 0, back = erofs_iblks(dir) - 1;
  82. unsigned int startprfx = 0, endprfx = 0;
  83. void *candidate = ERR_PTR(-ENOENT);
  84. while (head <= back) {
  85. const int mid = head + (back - head) / 2;
  86. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  87. struct erofs_dirent *de;
  88. buf.mapping = dir->i_mapping;
  89. de = erofs_bread(&buf, erofs_pos(dir->i_sb, mid), EROFS_KMAP);
  90. if (!IS_ERR(de)) {
  91. const int nameoff = nameoff_from_disk(de->nameoff, bsz);
  92. const int ndirents = nameoff / sizeof(*de);
  93. int diff;
  94. unsigned int matched;
  95. struct erofs_qstr dname;
  96. if (!ndirents) {
  97. erofs_put_metabuf(&buf);
  98. erofs_err(dir->i_sb,
  99. "corrupted dir block %d @ nid %llu",
  100. mid, EROFS_I(dir)->nid);
  101. DBG_BUGON(1);
  102. de = ERR_PTR(-EFSCORRUPTED);
  103. goto out;
  104. }
  105. matched = min(startprfx, endprfx);
  106. dname.name = (u8 *)de + nameoff;
  107. if (ndirents == 1)
  108. dname.end = (u8 *)de + bsz;
  109. else
  110. dname.end = (u8 *)de +
  111. nameoff_from_disk(de[1].nameoff, bsz);
  112. /* string comparison without already matched prefix */
  113. diff = erofs_dirnamecmp(name, &dname, &matched);
  114. if (diff < 0) {
  115. erofs_put_metabuf(&buf);
  116. back = mid - 1;
  117. endprfx = matched;
  118. continue;
  119. }
  120. if (!IS_ERR(candidate))
  121. erofs_put_metabuf(target);
  122. *target = buf;
  123. if (!diff) {
  124. *_ndirents = 0;
  125. return de;
  126. }
  127. head = mid + 1;
  128. startprfx = matched;
  129. candidate = de;
  130. *_ndirents = ndirents;
  131. continue;
  132. }
  133. out: /* free if the candidate is valid */
  134. if (!IS_ERR(candidate))
  135. erofs_put_metabuf(target);
  136. return de;
  137. }
  138. return candidate;
  139. }
  140. int erofs_namei(struct inode *dir, const struct qstr *name, erofs_nid_t *nid,
  141. unsigned int *d_type)
  142. {
  143. int ndirents;
  144. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  145. struct erofs_dirent *de;
  146. struct erofs_qstr qn;
  147. if (!dir->i_size)
  148. return -ENOENT;
  149. qn.name = name->name;
  150. qn.end = name->name + name->len;
  151. buf.mapping = dir->i_mapping;
  152. ndirents = 0;
  153. de = erofs_find_target_block(&buf, dir, &qn, &ndirents);
  154. if (IS_ERR(de))
  155. return PTR_ERR(de);
  156. if (ndirents)
  157. de = find_target_dirent(&qn, (u8 *)de, i_blocksize(dir),
  158. ndirents);
  159. if (!IS_ERR(de)) {
  160. *nid = le64_to_cpu(de->nid);
  161. *d_type = de->file_type;
  162. }
  163. erofs_put_metabuf(&buf);
  164. return PTR_ERR_OR_ZERO(de);
  165. }
  166. static struct dentry *erofs_lookup(struct inode *dir, struct dentry *dentry,
  167. unsigned int flags)
  168. {
  169. int err;
  170. erofs_nid_t nid;
  171. unsigned int d_type;
  172. struct inode *inode;
  173. trace_erofs_lookup(dir, dentry, flags);
  174. if (dentry->d_name.len > EROFS_NAME_LEN)
  175. return ERR_PTR(-ENAMETOOLONG);
  176. err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
  177. if (err == -ENOENT)
  178. /* negative dentry */
  179. inode = NULL;
  180. else if (err)
  181. inode = ERR_PTR(err);
  182. else
  183. inode = erofs_iget(dir->i_sb, nid);
  184. return d_splice_alias(inode, dentry);
  185. }
  186. const struct inode_operations erofs_dir_iops = {
  187. .lookup = erofs_lookup,
  188. .getattr = erofs_getattr,
  189. .listxattr = erofs_listxattr,
  190. .get_inode_acl = erofs_get_acl,
  191. .fiemap = erofs_fiemap,
  192. };