dir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * linux/fs/minix/dir.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * minix directory handling functions
  7. *
  8. * Updated to filesystem version 3 by Daniel Aragones
  9. */
  10. #include "minix.h"
  11. #include <linux/buffer_head.h>
  12. #include <linux/highmem.h>
  13. #include <linux/swap.h>
  14. typedef struct minix_dir_entry minix_dirent;
  15. typedef struct minix3_dir_entry minix3_dirent;
  16. static int minix_readdir(struct file *, struct dir_context *);
  17. const struct file_operations minix_dir_operations = {
  18. .llseek = generic_file_llseek,
  19. .read = generic_read_dir,
  20. .iterate_shared = minix_readdir,
  21. .fsync = generic_file_fsync,
  22. };
  23. static inline void dir_put_page(struct page *page)
  24. {
  25. kunmap(page);
  26. put_page(page);
  27. }
  28. /*
  29. * Return the offset into page `page_nr' of the last valid
  30. * byte in that page, plus one.
  31. */
  32. static unsigned
  33. minix_last_byte(struct inode *inode, unsigned long page_nr)
  34. {
  35. unsigned last_byte = PAGE_SIZE;
  36. if (page_nr == (inode->i_size >> PAGE_SHIFT))
  37. last_byte = inode->i_size & (PAGE_SIZE - 1);
  38. return last_byte;
  39. }
  40. static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
  41. {
  42. struct address_space *mapping = page->mapping;
  43. struct inode *dir = mapping->host;
  44. int err = 0;
  45. block_write_end(NULL, mapping, pos, len, len, page, NULL);
  46. if (pos+len > dir->i_size) {
  47. i_size_write(dir, pos+len);
  48. mark_inode_dirty(dir);
  49. }
  50. if (IS_DIRSYNC(dir))
  51. err = write_one_page(page, 1);
  52. else
  53. unlock_page(page);
  54. return err;
  55. }
  56. static struct page * dir_get_page(struct inode *dir, unsigned long n)
  57. {
  58. struct address_space *mapping = dir->i_mapping;
  59. struct page *page = read_mapping_page(mapping, n, NULL);
  60. if (!IS_ERR(page))
  61. kmap(page);
  62. return page;
  63. }
  64. static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
  65. {
  66. return (void*)((char*)de + sbi->s_dirsize);
  67. }
  68. static int minix_readdir(struct file *file, struct dir_context *ctx)
  69. {
  70. struct inode *inode = file_inode(file);
  71. struct super_block *sb = inode->i_sb;
  72. struct minix_sb_info *sbi = minix_sb(sb);
  73. unsigned chunk_size = sbi->s_dirsize;
  74. unsigned long npages = dir_pages(inode);
  75. unsigned long pos = ctx->pos;
  76. unsigned offset;
  77. unsigned long n;
  78. ctx->pos = pos = ALIGN(pos, chunk_size);
  79. if (pos >= inode->i_size)
  80. return 0;
  81. offset = pos & ~PAGE_MASK;
  82. n = pos >> PAGE_SHIFT;
  83. for ( ; n < npages; n++, offset = 0) {
  84. char *p, *kaddr, *limit;
  85. struct page *page = dir_get_page(inode, n);
  86. if (IS_ERR(page))
  87. continue;
  88. kaddr = (char *)page_address(page);
  89. p = kaddr+offset;
  90. limit = kaddr + minix_last_byte(inode, n) - chunk_size;
  91. for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
  92. const char *name;
  93. __u32 inumber;
  94. if (sbi->s_version == MINIX_V3) {
  95. minix3_dirent *de3 = (minix3_dirent *)p;
  96. name = de3->name;
  97. inumber = de3->inode;
  98. } else {
  99. minix_dirent *de = (minix_dirent *)p;
  100. name = de->name;
  101. inumber = de->inode;
  102. }
  103. if (inumber) {
  104. unsigned l = strnlen(name, sbi->s_namelen);
  105. if (!dir_emit(ctx, name, l,
  106. inumber, DT_UNKNOWN)) {
  107. dir_put_page(page);
  108. return 0;
  109. }
  110. }
  111. ctx->pos += chunk_size;
  112. }
  113. dir_put_page(page);
  114. }
  115. return 0;
  116. }
  117. static inline int namecompare(int len, int maxlen,
  118. const char * name, const char * buffer)
  119. {
  120. if (len < maxlen && buffer[len])
  121. return 0;
  122. return !memcmp(name, buffer, len);
  123. }
  124. /*
  125. * minix_find_entry()
  126. *
  127. * finds an entry in the specified directory with the wanted name. It
  128. * returns the cache buffer in which the entry was found, and the entry
  129. * itself (as a parameter - res_dir). It does NOT read the inode of the
  130. * entry - you'll have to do that yourself if you want to.
  131. */
  132. minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
  133. {
  134. const char * name = dentry->d_name.name;
  135. int namelen = dentry->d_name.len;
  136. struct inode * dir = d_inode(dentry->d_parent);
  137. struct super_block * sb = dir->i_sb;
  138. struct minix_sb_info * sbi = minix_sb(sb);
  139. unsigned long n;
  140. unsigned long npages = dir_pages(dir);
  141. struct page *page = NULL;
  142. char *p;
  143. char *namx;
  144. __u32 inumber;
  145. *res_page = NULL;
  146. for (n = 0; n < npages; n++) {
  147. char *kaddr, *limit;
  148. page = dir_get_page(dir, n);
  149. if (IS_ERR(page))
  150. continue;
  151. kaddr = (char*)page_address(page);
  152. limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
  153. for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
  154. if (sbi->s_version == MINIX_V3) {
  155. minix3_dirent *de3 = (minix3_dirent *)p;
  156. namx = de3->name;
  157. inumber = de3->inode;
  158. } else {
  159. minix_dirent *de = (minix_dirent *)p;
  160. namx = de->name;
  161. inumber = de->inode;
  162. }
  163. if (!inumber)
  164. continue;
  165. if (namecompare(namelen, sbi->s_namelen, name, namx))
  166. goto found;
  167. }
  168. dir_put_page(page);
  169. }
  170. return NULL;
  171. found:
  172. *res_page = page;
  173. return (minix_dirent *)p;
  174. }
  175. int minix_add_link(struct dentry *dentry, struct inode *inode)
  176. {
  177. struct inode *dir = d_inode(dentry->d_parent);
  178. const char * name = dentry->d_name.name;
  179. int namelen = dentry->d_name.len;
  180. struct super_block * sb = dir->i_sb;
  181. struct minix_sb_info * sbi = minix_sb(sb);
  182. struct page *page = NULL;
  183. unsigned long npages = dir_pages(dir);
  184. unsigned long n;
  185. char *kaddr, *p;
  186. minix_dirent *de;
  187. minix3_dirent *de3;
  188. loff_t pos;
  189. int err;
  190. char *namx = NULL;
  191. __u32 inumber;
  192. /*
  193. * We take care of directory expansion in the same loop
  194. * This code plays outside i_size, so it locks the page
  195. * to protect that region.
  196. */
  197. for (n = 0; n <= npages; n++) {
  198. char *limit, *dir_end;
  199. page = dir_get_page(dir, n);
  200. err = PTR_ERR(page);
  201. if (IS_ERR(page))
  202. goto out;
  203. lock_page(page);
  204. kaddr = (char*)page_address(page);
  205. dir_end = kaddr + minix_last_byte(dir, n);
  206. limit = kaddr + PAGE_SIZE - sbi->s_dirsize;
  207. for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
  208. de = (minix_dirent *)p;
  209. de3 = (minix3_dirent *)p;
  210. if (sbi->s_version == MINIX_V3) {
  211. namx = de3->name;
  212. inumber = de3->inode;
  213. } else {
  214. namx = de->name;
  215. inumber = de->inode;
  216. }
  217. if (p == dir_end) {
  218. /* We hit i_size */
  219. if (sbi->s_version == MINIX_V3)
  220. de3->inode = 0;
  221. else
  222. de->inode = 0;
  223. goto got_it;
  224. }
  225. if (!inumber)
  226. goto got_it;
  227. err = -EEXIST;
  228. if (namecompare(namelen, sbi->s_namelen, name, namx))
  229. goto out_unlock;
  230. }
  231. unlock_page(page);
  232. dir_put_page(page);
  233. }
  234. BUG();
  235. return -EINVAL;
  236. got_it:
  237. pos = page_offset(page) + p - (char *)page_address(page);
  238. err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
  239. if (err)
  240. goto out_unlock;
  241. memcpy (namx, name, namelen);
  242. if (sbi->s_version == MINIX_V3) {
  243. memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
  244. de3->inode = inode->i_ino;
  245. } else {
  246. memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
  247. de->inode = inode->i_ino;
  248. }
  249. err = dir_commit_chunk(page, pos, sbi->s_dirsize);
  250. dir->i_mtime = dir->i_ctime = current_time(dir);
  251. mark_inode_dirty(dir);
  252. out_put:
  253. dir_put_page(page);
  254. out:
  255. return err;
  256. out_unlock:
  257. unlock_page(page);
  258. goto out_put;
  259. }
  260. int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
  261. {
  262. struct inode *inode = page->mapping->host;
  263. char *kaddr = page_address(page);
  264. loff_t pos = page_offset(page) + (char*)de - kaddr;
  265. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  266. unsigned len = sbi->s_dirsize;
  267. int err;
  268. lock_page(page);
  269. err = minix_prepare_chunk(page, pos, len);
  270. if (err == 0) {
  271. if (sbi->s_version == MINIX_V3)
  272. ((minix3_dirent *) de)->inode = 0;
  273. else
  274. de->inode = 0;
  275. err = dir_commit_chunk(page, pos, len);
  276. } else {
  277. unlock_page(page);
  278. }
  279. dir_put_page(page);
  280. inode->i_ctime = inode->i_mtime = current_time(inode);
  281. mark_inode_dirty(inode);
  282. return err;
  283. }
  284. int minix_make_empty(struct inode *inode, struct inode *dir)
  285. {
  286. struct page *page = grab_cache_page(inode->i_mapping, 0);
  287. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  288. char *kaddr;
  289. int err;
  290. if (!page)
  291. return -ENOMEM;
  292. err = minix_prepare_chunk(page, 0, 2 * sbi->s_dirsize);
  293. if (err) {
  294. unlock_page(page);
  295. goto fail;
  296. }
  297. kaddr = kmap_atomic(page);
  298. memset(kaddr, 0, PAGE_SIZE);
  299. if (sbi->s_version == MINIX_V3) {
  300. minix3_dirent *de3 = (minix3_dirent *)kaddr;
  301. de3->inode = inode->i_ino;
  302. strcpy(de3->name, ".");
  303. de3 = minix_next_entry(de3, sbi);
  304. de3->inode = dir->i_ino;
  305. strcpy(de3->name, "..");
  306. } else {
  307. minix_dirent *de = (minix_dirent *)kaddr;
  308. de->inode = inode->i_ino;
  309. strcpy(de->name, ".");
  310. de = minix_next_entry(de, sbi);
  311. de->inode = dir->i_ino;
  312. strcpy(de->name, "..");
  313. }
  314. kunmap_atomic(kaddr);
  315. err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
  316. fail:
  317. put_page(page);
  318. return err;
  319. }
  320. /*
  321. * routine to check that the specified directory is empty (for rmdir)
  322. */
  323. int minix_empty_dir(struct inode * inode)
  324. {
  325. struct page *page = NULL;
  326. unsigned long i, npages = dir_pages(inode);
  327. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  328. char *name;
  329. __u32 inumber;
  330. for (i = 0; i < npages; i++) {
  331. char *p, *kaddr, *limit;
  332. page = dir_get_page(inode, i);
  333. if (IS_ERR(page))
  334. continue;
  335. kaddr = (char *)page_address(page);
  336. limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
  337. for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
  338. if (sbi->s_version == MINIX_V3) {
  339. minix3_dirent *de3 = (minix3_dirent *)p;
  340. name = de3->name;
  341. inumber = de3->inode;
  342. } else {
  343. minix_dirent *de = (minix_dirent *)p;
  344. name = de->name;
  345. inumber = de->inode;
  346. }
  347. if (inumber != 0) {
  348. /* check for . and .. */
  349. if (name[0] != '.')
  350. goto not_empty;
  351. if (!name[1]) {
  352. if (inumber != inode->i_ino)
  353. goto not_empty;
  354. } else if (name[1] != '.')
  355. goto not_empty;
  356. else if (name[2])
  357. goto not_empty;
  358. }
  359. }
  360. dir_put_page(page);
  361. }
  362. return 1;
  363. not_empty:
  364. dir_put_page(page);
  365. return 0;
  366. }
  367. /* Releases the page */
  368. void minix_set_link(struct minix_dir_entry *de, struct page *page,
  369. struct inode *inode)
  370. {
  371. struct inode *dir = page->mapping->host;
  372. struct minix_sb_info *sbi = minix_sb(dir->i_sb);
  373. loff_t pos = page_offset(page) +
  374. (char *)de-(char*)page_address(page);
  375. int err;
  376. lock_page(page);
  377. err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
  378. if (err == 0) {
  379. if (sbi->s_version == MINIX_V3)
  380. ((minix3_dirent *) de)->inode = inode->i_ino;
  381. else
  382. de->inode = inode->i_ino;
  383. err = dir_commit_chunk(page, pos, sbi->s_dirsize);
  384. } else {
  385. unlock_page(page);
  386. }
  387. dir_put_page(page);
  388. dir->i_mtime = dir->i_ctime = current_time(dir);
  389. mark_inode_dirty(dir);
  390. }
  391. struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
  392. {
  393. struct page *page = dir_get_page(dir, 0);
  394. struct minix_sb_info *sbi = minix_sb(dir->i_sb);
  395. struct minix_dir_entry *de = NULL;
  396. if (!IS_ERR(page)) {
  397. de = minix_next_entry(page_address(page), sbi);
  398. *p = page;
  399. }
  400. return de;
  401. }
  402. ino_t minix_inode_by_name(struct dentry *dentry)
  403. {
  404. struct page *page;
  405. struct minix_dir_entry *de = minix_find_entry(dentry, &page);
  406. ino_t res = 0;
  407. if (de) {
  408. struct address_space *mapping = page->mapping;
  409. struct inode *inode = mapping->host;
  410. struct minix_sb_info *sbi = minix_sb(inode->i_sb);
  411. if (sbi->s_version == MINIX_V3)
  412. res = ((minix3_dirent *) de)->inode;
  413. else
  414. res = de->inode;
  415. dir_put_page(page);
  416. }
  417. return res;
  418. }