dir.c 8.7 KB

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