dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * linux/fs/hfsplus/dir.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handling of directories
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/fs.h>
  12. #include <linux/slab.h>
  13. #include <linux/random.h>
  14. #include <linux/nls.h>
  15. #include "hfsplus_fs.h"
  16. #include "hfsplus_raw.h"
  17. #include "xattr.h"
  18. #include "acl.h"
  19. static inline void hfsplus_instantiate(struct dentry *dentry,
  20. struct inode *inode, u32 cnid)
  21. {
  22. dentry->d_fsdata = (void *)(unsigned long)cnid;
  23. d_instantiate(dentry, inode);
  24. }
  25. /* Find the entry inside dir named dentry->d_name */
  26. static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
  27. unsigned int flags)
  28. {
  29. struct inode *inode = NULL;
  30. struct hfs_find_data fd;
  31. struct super_block *sb;
  32. hfsplus_cat_entry entry;
  33. int err;
  34. u32 cnid, linkid = 0;
  35. u16 type;
  36. sb = dir->i_sb;
  37. dentry->d_fsdata = NULL;
  38. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  39. if (err)
  40. return ERR_PTR(err);
  41. err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino,
  42. &dentry->d_name);
  43. if (unlikely(err < 0))
  44. goto fail;
  45. again:
  46. err = hfs_brec_read(&fd, &entry, sizeof(entry));
  47. if (err) {
  48. if (err == -ENOENT) {
  49. hfs_find_exit(&fd);
  50. /* No such entry */
  51. inode = NULL;
  52. goto out;
  53. }
  54. goto fail;
  55. }
  56. type = be16_to_cpu(entry.type);
  57. if (type == HFSPLUS_FOLDER) {
  58. if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
  59. err = -EIO;
  60. goto fail;
  61. }
  62. cnid = be32_to_cpu(entry.folder.id);
  63. dentry->d_fsdata = (void *)(unsigned long)cnid;
  64. } else if (type == HFSPLUS_FILE) {
  65. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  66. err = -EIO;
  67. goto fail;
  68. }
  69. cnid = be32_to_cpu(entry.file.id);
  70. if (entry.file.user_info.fdType ==
  71. cpu_to_be32(HFSP_HARDLINK_TYPE) &&
  72. entry.file.user_info.fdCreator ==
  73. cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
  74. (entry.file.create_date ==
  75. HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
  76. create_date ||
  77. entry.file.create_date ==
  78. HFSPLUS_I(d_inode(sb->s_root))->
  79. create_date) &&
  80. HFSPLUS_SB(sb)->hidden_dir) {
  81. struct qstr str;
  82. char name[32];
  83. if (dentry->d_fsdata) {
  84. /*
  85. * We found a link pointing to another link,
  86. * so ignore it and treat it as regular file.
  87. */
  88. cnid = (unsigned long)dentry->d_fsdata;
  89. linkid = 0;
  90. } else {
  91. dentry->d_fsdata = (void *)(unsigned long)cnid;
  92. linkid =
  93. be32_to_cpu(entry.file.permissions.dev);
  94. str.len = sprintf(name, "iNode%d", linkid);
  95. str.name = name;
  96. err = hfsplus_cat_build_key(sb, fd.search_key,
  97. HFSPLUS_SB(sb)->hidden_dir->i_ino,
  98. &str);
  99. if (unlikely(err < 0))
  100. goto fail;
  101. goto again;
  102. }
  103. } else if (!dentry->d_fsdata)
  104. dentry->d_fsdata = (void *)(unsigned long)cnid;
  105. } else {
  106. pr_err("invalid catalog entry type in lookup\n");
  107. err = -EIO;
  108. goto fail;
  109. }
  110. hfs_find_exit(&fd);
  111. inode = hfsplus_iget(dir->i_sb, cnid);
  112. if (IS_ERR(inode))
  113. return ERR_CAST(inode);
  114. if (S_ISREG(inode->i_mode))
  115. HFSPLUS_I(inode)->linkid = linkid;
  116. out:
  117. d_add(dentry, inode);
  118. return NULL;
  119. fail:
  120. hfs_find_exit(&fd);
  121. return ERR_PTR(err);
  122. }
  123. static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
  124. {
  125. struct inode *inode = file_inode(file);
  126. struct super_block *sb = inode->i_sb;
  127. int len, err;
  128. char *strbuf;
  129. hfsplus_cat_entry entry;
  130. struct hfs_find_data fd;
  131. struct hfsplus_readdir_data *rd;
  132. u16 type;
  133. if (file->f_pos >= inode->i_size)
  134. return 0;
  135. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  136. if (err)
  137. return err;
  138. strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
  139. if (!strbuf) {
  140. err = -ENOMEM;
  141. goto out;
  142. }
  143. hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino);
  144. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  145. if (err)
  146. goto out;
  147. if (ctx->pos == 0) {
  148. /* This is completely artificial... */
  149. if (!dir_emit_dot(file, ctx))
  150. goto out;
  151. ctx->pos = 1;
  152. }
  153. if (ctx->pos == 1) {
  154. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  155. err = -EIO;
  156. goto out;
  157. }
  158. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  159. fd.entrylength);
  160. if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
  161. pr_err("bad catalog folder thread\n");
  162. err = -EIO;
  163. goto out;
  164. }
  165. if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
  166. pr_err("truncated catalog thread\n");
  167. err = -EIO;
  168. goto out;
  169. }
  170. if (!dir_emit(ctx, "..", 2,
  171. be32_to_cpu(entry.thread.parentID), DT_DIR))
  172. goto out;
  173. ctx->pos = 2;
  174. }
  175. if (ctx->pos >= inode->i_size)
  176. goto out;
  177. err = hfs_brec_goto(&fd, ctx->pos - 1);
  178. if (err)
  179. goto out;
  180. for (;;) {
  181. if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
  182. pr_err("walked past end of dir\n");
  183. err = -EIO;
  184. goto out;
  185. }
  186. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  187. err = -EIO;
  188. goto out;
  189. }
  190. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  191. fd.entrylength);
  192. type = be16_to_cpu(entry.type);
  193. len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
  194. err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
  195. if (err)
  196. goto out;
  197. if (type == HFSPLUS_FOLDER) {
  198. if (fd.entrylength <
  199. sizeof(struct hfsplus_cat_folder)) {
  200. pr_err("small dir entry\n");
  201. err = -EIO;
  202. goto out;
  203. }
  204. if (HFSPLUS_SB(sb)->hidden_dir &&
  205. HFSPLUS_SB(sb)->hidden_dir->i_ino ==
  206. be32_to_cpu(entry.folder.id))
  207. goto next;
  208. if (!dir_emit(ctx, strbuf, len,
  209. be32_to_cpu(entry.folder.id), DT_DIR))
  210. break;
  211. } else if (type == HFSPLUS_FILE) {
  212. u16 mode;
  213. unsigned type = DT_UNKNOWN;
  214. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  215. pr_err("small file entry\n");
  216. err = -EIO;
  217. goto out;
  218. }
  219. mode = be16_to_cpu(entry.file.permissions.mode);
  220. if (S_ISREG(mode))
  221. type = DT_REG;
  222. else if (S_ISLNK(mode))
  223. type = DT_LNK;
  224. else if (S_ISFIFO(mode))
  225. type = DT_FIFO;
  226. else if (S_ISCHR(mode))
  227. type = DT_CHR;
  228. else if (S_ISBLK(mode))
  229. type = DT_BLK;
  230. else if (S_ISSOCK(mode))
  231. type = DT_SOCK;
  232. if (!dir_emit(ctx, strbuf, len,
  233. be32_to_cpu(entry.file.id), type))
  234. break;
  235. } else {
  236. pr_err("bad catalog entry type\n");
  237. err = -EIO;
  238. goto out;
  239. }
  240. next:
  241. ctx->pos++;
  242. if (ctx->pos >= inode->i_size)
  243. goto out;
  244. err = hfs_brec_goto(&fd, 1);
  245. if (err)
  246. goto out;
  247. }
  248. rd = file->private_data;
  249. if (!rd) {
  250. rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
  251. if (!rd) {
  252. err = -ENOMEM;
  253. goto out;
  254. }
  255. file->private_data = rd;
  256. rd->file = file;
  257. spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
  258. list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
  259. spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
  260. }
  261. /*
  262. * Can be done after the list insertion; exclusion with
  263. * hfsplus_delete_cat() is provided by directory lock.
  264. */
  265. memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
  266. out:
  267. kfree(strbuf);
  268. hfs_find_exit(&fd);
  269. return err;
  270. }
  271. static int hfsplus_dir_release(struct inode *inode, struct file *file)
  272. {
  273. struct hfsplus_readdir_data *rd = file->private_data;
  274. if (rd) {
  275. spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
  276. list_del(&rd->list);
  277. spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
  278. kfree(rd);
  279. }
  280. return 0;
  281. }
  282. static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
  283. struct dentry *dst_dentry)
  284. {
  285. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
  286. struct inode *inode = d_inode(src_dentry);
  287. struct inode *src_dir = d_inode(src_dentry->d_parent);
  288. struct qstr str;
  289. char name[32];
  290. u32 cnid, id;
  291. int res;
  292. if (HFSPLUS_IS_RSRC(inode))
  293. return -EPERM;
  294. if (!S_ISREG(inode->i_mode))
  295. return -EPERM;
  296. mutex_lock(&sbi->vh_mutex);
  297. if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
  298. for (;;) {
  299. get_random_bytes(&id, sizeof(cnid));
  300. id &= 0x3fffffff;
  301. str.name = name;
  302. str.len = sprintf(name, "iNode%d", id);
  303. res = hfsplus_rename_cat(inode->i_ino,
  304. src_dir, &src_dentry->d_name,
  305. sbi->hidden_dir, &str);
  306. if (!res)
  307. break;
  308. if (res != -EEXIST)
  309. goto out;
  310. }
  311. HFSPLUS_I(inode)->linkid = id;
  312. cnid = sbi->next_cnid++;
  313. src_dentry->d_fsdata = (void *)(unsigned long)cnid;
  314. res = hfsplus_create_cat(cnid, src_dir,
  315. &src_dentry->d_name, inode);
  316. if (res)
  317. /* panic? */
  318. goto out;
  319. sbi->file_count++;
  320. }
  321. cnid = sbi->next_cnid++;
  322. res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
  323. if (res)
  324. goto out;
  325. inc_nlink(inode);
  326. hfsplus_instantiate(dst_dentry, inode, cnid);
  327. ihold(inode);
  328. inode->i_ctime = current_time(inode);
  329. mark_inode_dirty(inode);
  330. sbi->file_count++;
  331. hfsplus_mark_mdb_dirty(dst_dir->i_sb);
  332. out:
  333. mutex_unlock(&sbi->vh_mutex);
  334. return res;
  335. }
  336. static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
  337. {
  338. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  339. struct inode *inode = d_inode(dentry);
  340. struct qstr str;
  341. char name[32];
  342. u32 cnid;
  343. int res;
  344. if (HFSPLUS_IS_RSRC(inode))
  345. return -EPERM;
  346. mutex_lock(&sbi->vh_mutex);
  347. cnid = (u32)(unsigned long)dentry->d_fsdata;
  348. if (inode->i_ino == cnid &&
  349. atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  350. str.name = name;
  351. str.len = sprintf(name, "temp%lu", inode->i_ino);
  352. res = hfsplus_rename_cat(inode->i_ino,
  353. dir, &dentry->d_name,
  354. sbi->hidden_dir, &str);
  355. if (!res) {
  356. inode->i_flags |= S_DEAD;
  357. drop_nlink(inode);
  358. }
  359. goto out;
  360. }
  361. res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
  362. if (res)
  363. goto out;
  364. if (inode->i_nlink > 0)
  365. drop_nlink(inode);
  366. if (inode->i_ino == cnid)
  367. clear_nlink(inode);
  368. if (!inode->i_nlink) {
  369. if (inode->i_ino != cnid) {
  370. sbi->file_count--;
  371. if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  372. res = hfsplus_delete_cat(inode->i_ino,
  373. sbi->hidden_dir,
  374. NULL);
  375. if (!res)
  376. hfsplus_delete_inode(inode);
  377. } else
  378. inode->i_flags |= S_DEAD;
  379. } else
  380. hfsplus_delete_inode(inode);
  381. } else
  382. sbi->file_count--;
  383. inode->i_ctime = current_time(inode);
  384. mark_inode_dirty(inode);
  385. out:
  386. mutex_unlock(&sbi->vh_mutex);
  387. return res;
  388. }
  389. static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
  390. {
  391. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  392. struct inode *inode = d_inode(dentry);
  393. int res;
  394. if (inode->i_size != 2)
  395. return -ENOTEMPTY;
  396. mutex_lock(&sbi->vh_mutex);
  397. res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  398. if (res)
  399. goto out;
  400. clear_nlink(inode);
  401. inode->i_ctime = current_time(inode);
  402. hfsplus_delete_inode(inode);
  403. mark_inode_dirty(inode);
  404. out:
  405. mutex_unlock(&sbi->vh_mutex);
  406. return res;
  407. }
  408. static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
  409. const char *symname)
  410. {
  411. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  412. struct inode *inode;
  413. int res = -ENOMEM;
  414. mutex_lock(&sbi->vh_mutex);
  415. inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
  416. if (!inode)
  417. goto out;
  418. res = page_symlink(inode, symname, strlen(symname) + 1);
  419. if (res)
  420. goto out_err;
  421. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  422. if (res)
  423. goto out_err;
  424. res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
  425. if (res == -EOPNOTSUPP)
  426. res = 0; /* Operation is not supported. */
  427. else if (res) {
  428. /* Try to delete anyway without error analysis. */
  429. hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  430. goto out_err;
  431. }
  432. hfsplus_instantiate(dentry, inode, inode->i_ino);
  433. mark_inode_dirty(inode);
  434. goto out;
  435. out_err:
  436. clear_nlink(inode);
  437. hfsplus_delete_inode(inode);
  438. iput(inode);
  439. out:
  440. mutex_unlock(&sbi->vh_mutex);
  441. return res;
  442. }
  443. static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
  444. umode_t mode, dev_t rdev)
  445. {
  446. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  447. struct inode *inode;
  448. int res = -ENOMEM;
  449. mutex_lock(&sbi->vh_mutex);
  450. inode = hfsplus_new_inode(dir->i_sb, mode);
  451. if (!inode)
  452. goto out;
  453. if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
  454. init_special_inode(inode, mode, rdev);
  455. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  456. if (res)
  457. goto failed_mknod;
  458. res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
  459. if (res == -EOPNOTSUPP)
  460. res = 0; /* Operation is not supported. */
  461. else if (res) {
  462. /* Try to delete anyway without error analysis. */
  463. hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  464. goto failed_mknod;
  465. }
  466. hfsplus_instantiate(dentry, inode, inode->i_ino);
  467. mark_inode_dirty(inode);
  468. goto out;
  469. failed_mknod:
  470. clear_nlink(inode);
  471. hfsplus_delete_inode(inode);
  472. iput(inode);
  473. out:
  474. mutex_unlock(&sbi->vh_mutex);
  475. return res;
  476. }
  477. static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  478. bool excl)
  479. {
  480. return hfsplus_mknod(dir, dentry, mode, 0);
  481. }
  482. static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  483. {
  484. return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
  485. }
  486. static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
  487. struct inode *new_dir, struct dentry *new_dentry,
  488. unsigned int flags)
  489. {
  490. int res;
  491. if (flags & ~RENAME_NOREPLACE)
  492. return -EINVAL;
  493. /* Unlink destination if it already exists */
  494. if (d_really_is_positive(new_dentry)) {
  495. if (d_is_dir(new_dentry))
  496. res = hfsplus_rmdir(new_dir, new_dentry);
  497. else
  498. res = hfsplus_unlink(new_dir, new_dentry);
  499. if (res)
  500. return res;
  501. }
  502. res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
  503. old_dir, &old_dentry->d_name,
  504. new_dir, &new_dentry->d_name);
  505. if (!res)
  506. new_dentry->d_fsdata = old_dentry->d_fsdata;
  507. return res;
  508. }
  509. const struct inode_operations hfsplus_dir_inode_operations = {
  510. .lookup = hfsplus_lookup,
  511. .create = hfsplus_create,
  512. .link = hfsplus_link,
  513. .unlink = hfsplus_unlink,
  514. .mkdir = hfsplus_mkdir,
  515. .rmdir = hfsplus_rmdir,
  516. .symlink = hfsplus_symlink,
  517. .mknod = hfsplus_mknod,
  518. .rename = hfsplus_rename,
  519. .listxattr = hfsplus_listxattr,
  520. #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
  521. .get_acl = hfsplus_get_posix_acl,
  522. .set_acl = hfsplus_set_posix_acl,
  523. #endif
  524. };
  525. const struct file_operations hfsplus_dir_operations = {
  526. .fsync = hfsplus_file_fsync,
  527. .read = generic_read_dir,
  528. .iterate_shared = hfsplus_readdir,
  529. .unlocked_ioctl = hfsplus_ioctl,
  530. .llseek = generic_file_llseek,
  531. .release = hfsplus_dir_release,
  532. };