dir.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/sysv/dir.c
  4. *
  5. * minix/dir.c
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * coh/dir.c
  9. * Copyright (C) 1993 Pascal Haible, Bruno Haible
  10. *
  11. * sysv/dir.c
  12. * Copyright (C) 1993 Bruno Haible
  13. *
  14. * SystemV/Coherent directory handling functions
  15. */
  16. #include <linux/pagemap.h>
  17. #include <linux/highmem.h>
  18. #include <linux/swap.h>
  19. #include "sysv.h"
  20. static int sysv_readdir(struct file *, struct dir_context *);
  21. const struct file_operations sysv_dir_operations = {
  22. .llseek = generic_file_llseek,
  23. .read = generic_read_dir,
  24. .iterate_shared = sysv_readdir,
  25. .fsync = generic_file_fsync,
  26. };
  27. static void dir_commit_chunk(struct folio *folio, loff_t pos, unsigned len)
  28. {
  29. struct address_space *mapping = folio->mapping;
  30. struct inode *dir = mapping->host;
  31. block_write_end(NULL, mapping, pos, len, len, folio, NULL);
  32. if (pos+len > dir->i_size) {
  33. i_size_write(dir, pos+len);
  34. mark_inode_dirty(dir);
  35. }
  36. folio_unlock(folio);
  37. }
  38. static int sysv_handle_dirsync(struct inode *dir)
  39. {
  40. int err;
  41. err = filemap_write_and_wait(dir->i_mapping);
  42. if (!err)
  43. err = sync_inode_metadata(dir, 1);
  44. return err;
  45. }
  46. /*
  47. * Calls to dir_get_folio()/folio_release_kmap() must be nested according to the
  48. * rules documented in mm/highmem.rst.
  49. *
  50. * NOTE: sysv_find_entry() and sysv_dotdot() act as calls to dir_get_folio()
  51. * and must be treated accordingly for nesting purposes.
  52. */
  53. static void *dir_get_folio(struct inode *dir, unsigned long n,
  54. struct folio **foliop)
  55. {
  56. struct folio *folio = read_mapping_folio(dir->i_mapping, n, NULL);
  57. if (IS_ERR(folio))
  58. return ERR_CAST(folio);
  59. *foliop = folio;
  60. return kmap_local_folio(folio, 0);
  61. }
  62. static int sysv_readdir(struct file *file, struct dir_context *ctx)
  63. {
  64. unsigned long pos = ctx->pos;
  65. struct inode *inode = file_inode(file);
  66. struct super_block *sb = inode->i_sb;
  67. unsigned long npages = dir_pages(inode);
  68. unsigned offset;
  69. unsigned long n;
  70. ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
  71. if (pos >= inode->i_size)
  72. return 0;
  73. offset = pos & ~PAGE_MASK;
  74. n = pos >> PAGE_SHIFT;
  75. for ( ; n < npages; n++, offset = 0) {
  76. char *kaddr, *limit;
  77. struct sysv_dir_entry *de;
  78. struct folio *folio;
  79. kaddr = dir_get_folio(inode, n, &folio);
  80. if (IS_ERR(kaddr))
  81. continue;
  82. de = (struct sysv_dir_entry *)(kaddr+offset);
  83. limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
  84. for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
  85. char *name = de->name;
  86. if (!de->inode)
  87. continue;
  88. if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
  89. fs16_to_cpu(SYSV_SB(sb), de->inode),
  90. DT_UNKNOWN)) {
  91. folio_release_kmap(folio, kaddr);
  92. return 0;
  93. }
  94. }
  95. folio_release_kmap(folio, kaddr);
  96. }
  97. return 0;
  98. }
  99. /* compare strings: name[0..len-1] (not zero-terminated) and
  100. * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
  101. */
  102. static inline int namecompare(int len, int maxlen,
  103. const char * name, const char * buffer)
  104. {
  105. if (len < maxlen && buffer[len])
  106. return 0;
  107. return !memcmp(name, buffer, len);
  108. }
  109. /*
  110. * sysv_find_entry()
  111. *
  112. * finds an entry in the specified directory with the wanted name.
  113. * It does NOT read the inode of the
  114. * entry - you'll have to do that yourself if you want to.
  115. *
  116. * On Success folio_release_kmap() should be called on *foliop.
  117. *
  118. * sysv_find_entry() acts as a call to dir_get_folio() and must be treated
  119. * accordingly for nesting purposes.
  120. */
  121. struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct folio **foliop)
  122. {
  123. const char * name = dentry->d_name.name;
  124. int namelen = dentry->d_name.len;
  125. struct inode * dir = d_inode(dentry->d_parent);
  126. unsigned long start, n;
  127. unsigned long npages = dir_pages(dir);
  128. struct sysv_dir_entry *de;
  129. start = SYSV_I(dir)->i_dir_start_lookup;
  130. if (start >= npages)
  131. start = 0;
  132. n = start;
  133. do {
  134. char *kaddr = dir_get_folio(dir, n, foliop);
  135. if (!IS_ERR(kaddr)) {
  136. de = (struct sysv_dir_entry *)kaddr;
  137. kaddr += folio_size(*foliop) - SYSV_DIRSIZE;
  138. for ( ; (char *) de <= kaddr ; de++) {
  139. if (!de->inode)
  140. continue;
  141. if (namecompare(namelen, SYSV_NAMELEN,
  142. name, de->name))
  143. goto found;
  144. }
  145. folio_release_kmap(*foliop, kaddr);
  146. }
  147. if (++n >= npages)
  148. n = 0;
  149. } while (n != start);
  150. return NULL;
  151. found:
  152. SYSV_I(dir)->i_dir_start_lookup = n;
  153. return de;
  154. }
  155. int sysv_add_link(struct dentry *dentry, struct inode *inode)
  156. {
  157. struct inode *dir = d_inode(dentry->d_parent);
  158. const char * name = dentry->d_name.name;
  159. int namelen = dentry->d_name.len;
  160. struct folio *folio = NULL;
  161. struct sysv_dir_entry * de;
  162. unsigned long npages = dir_pages(dir);
  163. unsigned long n;
  164. char *kaddr;
  165. loff_t pos;
  166. int err;
  167. /* We take care of directory expansion in the same loop */
  168. for (n = 0; n <= npages; n++) {
  169. kaddr = dir_get_folio(dir, n, &folio);
  170. if (IS_ERR(kaddr))
  171. return PTR_ERR(kaddr);
  172. de = (struct sysv_dir_entry *)kaddr;
  173. kaddr += PAGE_SIZE - SYSV_DIRSIZE;
  174. while ((char *)de <= kaddr) {
  175. if (!de->inode)
  176. goto got_it;
  177. err = -EEXIST;
  178. if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
  179. goto out_folio;
  180. de++;
  181. }
  182. folio_release_kmap(folio, kaddr);
  183. }
  184. BUG();
  185. return -EINVAL;
  186. got_it:
  187. pos = folio_pos(folio) + offset_in_folio(folio, de);
  188. folio_lock(folio);
  189. err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
  190. if (err)
  191. goto out_unlock;
  192. memcpy (de->name, name, namelen);
  193. memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
  194. de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
  195. dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
  196. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  197. mark_inode_dirty(dir);
  198. err = sysv_handle_dirsync(dir);
  199. out_folio:
  200. folio_release_kmap(folio, kaddr);
  201. return err;
  202. out_unlock:
  203. folio_unlock(folio);
  204. goto out_folio;
  205. }
  206. int sysv_delete_entry(struct sysv_dir_entry *de, struct folio *folio)
  207. {
  208. struct inode *inode = folio->mapping->host;
  209. loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
  210. int err;
  211. folio_lock(folio);
  212. err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
  213. if (err) {
  214. folio_unlock(folio);
  215. return err;
  216. }
  217. de->inode = 0;
  218. dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
  219. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  220. mark_inode_dirty(inode);
  221. return sysv_handle_dirsync(inode);
  222. }
  223. int sysv_make_empty(struct inode *inode, struct inode *dir)
  224. {
  225. struct folio *folio = filemap_grab_folio(inode->i_mapping, 0);
  226. struct sysv_dir_entry * de;
  227. char *kaddr;
  228. int err;
  229. if (IS_ERR(folio))
  230. return PTR_ERR(folio);
  231. err = sysv_prepare_chunk(folio, 0, 2 * SYSV_DIRSIZE);
  232. if (err) {
  233. folio_unlock(folio);
  234. goto fail;
  235. }
  236. kaddr = kmap_local_folio(folio, 0);
  237. memset(kaddr, 0, folio_size(folio));
  238. de = (struct sysv_dir_entry *)kaddr;
  239. de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
  240. strcpy(de->name,".");
  241. de++;
  242. de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
  243. strcpy(de->name,"..");
  244. kunmap_local(kaddr);
  245. dir_commit_chunk(folio, 0, 2 * SYSV_DIRSIZE);
  246. err = sysv_handle_dirsync(inode);
  247. fail:
  248. folio_put(folio);
  249. return err;
  250. }
  251. /*
  252. * routine to check that the specified directory is empty (for rmdir)
  253. */
  254. int sysv_empty_dir(struct inode * inode)
  255. {
  256. struct super_block *sb = inode->i_sb;
  257. struct folio *folio = NULL;
  258. unsigned long i, npages = dir_pages(inode);
  259. char *kaddr;
  260. for (i = 0; i < npages; i++) {
  261. struct sysv_dir_entry *de;
  262. kaddr = dir_get_folio(inode, i, &folio);
  263. if (IS_ERR(kaddr))
  264. continue;
  265. de = (struct sysv_dir_entry *)kaddr;
  266. kaddr += folio_size(folio) - SYSV_DIRSIZE;
  267. for ( ;(char *)de <= kaddr; de++) {
  268. if (!de->inode)
  269. continue;
  270. /* check for . and .. */
  271. if (de->name[0] != '.')
  272. goto not_empty;
  273. if (!de->name[1]) {
  274. if (de->inode == cpu_to_fs16(SYSV_SB(sb),
  275. inode->i_ino))
  276. continue;
  277. goto not_empty;
  278. }
  279. if (de->name[1] != '.' || de->name[2])
  280. goto not_empty;
  281. }
  282. folio_release_kmap(folio, kaddr);
  283. }
  284. return 1;
  285. not_empty:
  286. folio_release_kmap(folio, kaddr);
  287. return 0;
  288. }
  289. /* Releases the page */
  290. int sysv_set_link(struct sysv_dir_entry *de, struct folio *folio,
  291. struct inode *inode)
  292. {
  293. struct inode *dir = folio->mapping->host;
  294. loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
  295. int err;
  296. folio_lock(folio);
  297. err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
  298. if (err) {
  299. folio_unlock(folio);
  300. return err;
  301. }
  302. de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
  303. dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
  304. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  305. mark_inode_dirty(dir);
  306. return sysv_handle_dirsync(inode);
  307. }
  308. /*
  309. * Calls to dir_get_folio()/folio_release_kmap() must be nested according to the
  310. * rules documented in mm/highmem.rst.
  311. *
  312. * sysv_dotdot() acts as a call to dir_get_folio() and must be treated
  313. * accordingly for nesting purposes.
  314. */
  315. struct sysv_dir_entry *sysv_dotdot(struct inode *dir, struct folio **foliop)
  316. {
  317. struct sysv_dir_entry *de = dir_get_folio(dir, 0, foliop);
  318. if (IS_ERR(de))
  319. return NULL;
  320. /* ".." is the second directory entry */
  321. return de + 1;
  322. }
  323. ino_t sysv_inode_by_name(struct dentry *dentry)
  324. {
  325. struct folio *folio;
  326. struct sysv_dir_entry *de = sysv_find_entry (dentry, &folio);
  327. ino_t res = 0;
  328. if (de) {
  329. res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
  330. folio_release_kmap(folio, de);
  331. }
  332. return res;
  333. }