dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
  258. }
  259. memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
  260. out:
  261. kfree(strbuf);
  262. hfs_find_exit(&fd);
  263. return err;
  264. }
  265. static int hfsplus_dir_release(struct inode *inode, struct file *file)
  266. {
  267. struct hfsplus_readdir_data *rd = file->private_data;
  268. if (rd) {
  269. mutex_lock(&inode->i_mutex);
  270. list_del(&rd->list);
  271. mutex_unlock(&inode->i_mutex);
  272. kfree(rd);
  273. }
  274. return 0;
  275. }
  276. static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
  277. struct dentry *dst_dentry)
  278. {
  279. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
  280. struct inode *inode = d_inode(src_dentry);
  281. struct inode *src_dir = d_inode(src_dentry->d_parent);
  282. struct qstr str;
  283. char name[32];
  284. u32 cnid, id;
  285. int res;
  286. if (HFSPLUS_IS_RSRC(inode))
  287. return -EPERM;
  288. if (!S_ISREG(inode->i_mode))
  289. return -EPERM;
  290. mutex_lock(&sbi->vh_mutex);
  291. if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
  292. for (;;) {
  293. get_random_bytes(&id, sizeof(cnid));
  294. id &= 0x3fffffff;
  295. str.name = name;
  296. str.len = sprintf(name, "iNode%d", id);
  297. res = hfsplus_rename_cat(inode->i_ino,
  298. src_dir, &src_dentry->d_name,
  299. sbi->hidden_dir, &str);
  300. if (!res)
  301. break;
  302. if (res != -EEXIST)
  303. goto out;
  304. }
  305. HFSPLUS_I(inode)->linkid = id;
  306. cnid = sbi->next_cnid++;
  307. src_dentry->d_fsdata = (void *)(unsigned long)cnid;
  308. res = hfsplus_create_cat(cnid, src_dir,
  309. &src_dentry->d_name, inode);
  310. if (res)
  311. /* panic? */
  312. goto out;
  313. sbi->file_count++;
  314. }
  315. cnid = sbi->next_cnid++;
  316. res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
  317. if (res)
  318. goto out;
  319. inc_nlink(inode);
  320. hfsplus_instantiate(dst_dentry, inode, cnid);
  321. ihold(inode);
  322. inode->i_ctime = CURRENT_TIME_SEC;
  323. mark_inode_dirty(inode);
  324. sbi->file_count++;
  325. hfsplus_mark_mdb_dirty(dst_dir->i_sb);
  326. out:
  327. mutex_unlock(&sbi->vh_mutex);
  328. return res;
  329. }
  330. static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
  331. {
  332. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  333. struct inode *inode = d_inode(dentry);
  334. struct qstr str;
  335. char name[32];
  336. u32 cnid;
  337. int res;
  338. if (HFSPLUS_IS_RSRC(inode))
  339. return -EPERM;
  340. mutex_lock(&sbi->vh_mutex);
  341. cnid = (u32)(unsigned long)dentry->d_fsdata;
  342. if (inode->i_ino == cnid &&
  343. atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  344. str.name = name;
  345. str.len = sprintf(name, "temp%lu", inode->i_ino);
  346. res = hfsplus_rename_cat(inode->i_ino,
  347. dir, &dentry->d_name,
  348. sbi->hidden_dir, &str);
  349. if (!res) {
  350. inode->i_flags |= S_DEAD;
  351. drop_nlink(inode);
  352. }
  353. goto out;
  354. }
  355. res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
  356. if (res)
  357. goto out;
  358. if (inode->i_nlink > 0)
  359. drop_nlink(inode);
  360. if (inode->i_ino == cnid)
  361. clear_nlink(inode);
  362. if (!inode->i_nlink) {
  363. if (inode->i_ino != cnid) {
  364. sbi->file_count--;
  365. if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  366. res = hfsplus_delete_cat(inode->i_ino,
  367. sbi->hidden_dir,
  368. NULL);
  369. if (!res)
  370. hfsplus_delete_inode(inode);
  371. } else
  372. inode->i_flags |= S_DEAD;
  373. } else
  374. hfsplus_delete_inode(inode);
  375. } else
  376. sbi->file_count--;
  377. inode->i_ctime = CURRENT_TIME_SEC;
  378. mark_inode_dirty(inode);
  379. out:
  380. mutex_unlock(&sbi->vh_mutex);
  381. return res;
  382. }
  383. static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
  384. {
  385. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  386. struct inode *inode = d_inode(dentry);
  387. int res;
  388. if (inode->i_size != 2)
  389. return -ENOTEMPTY;
  390. mutex_lock(&sbi->vh_mutex);
  391. res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  392. if (res)
  393. goto out;
  394. clear_nlink(inode);
  395. inode->i_ctime = CURRENT_TIME_SEC;
  396. hfsplus_delete_inode(inode);
  397. mark_inode_dirty(inode);
  398. out:
  399. mutex_unlock(&sbi->vh_mutex);
  400. return res;
  401. }
  402. static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
  403. const char *symname)
  404. {
  405. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  406. struct inode *inode;
  407. int res = -ENOMEM;
  408. mutex_lock(&sbi->vh_mutex);
  409. inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
  410. if (!inode)
  411. goto out;
  412. res = page_symlink(inode, symname, strlen(symname) + 1);
  413. if (res)
  414. goto out_err;
  415. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  416. if (res)
  417. goto out_err;
  418. res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
  419. if (res == -EOPNOTSUPP)
  420. res = 0; /* Operation is not supported. */
  421. else if (res) {
  422. /* Try to delete anyway without error analysis. */
  423. hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  424. goto out_err;
  425. }
  426. hfsplus_instantiate(dentry, inode, inode->i_ino);
  427. mark_inode_dirty(inode);
  428. goto out;
  429. out_err:
  430. clear_nlink(inode);
  431. hfsplus_delete_inode(inode);
  432. iput(inode);
  433. out:
  434. mutex_unlock(&sbi->vh_mutex);
  435. return res;
  436. }
  437. static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
  438. umode_t mode, dev_t rdev)
  439. {
  440. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  441. struct inode *inode;
  442. int res = -ENOMEM;
  443. mutex_lock(&sbi->vh_mutex);
  444. inode = hfsplus_new_inode(dir->i_sb, mode);
  445. if (!inode)
  446. goto out;
  447. if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
  448. init_special_inode(inode, mode, rdev);
  449. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  450. if (res)
  451. goto failed_mknod;
  452. res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
  453. if (res == -EOPNOTSUPP)
  454. res = 0; /* Operation is not supported. */
  455. else if (res) {
  456. /* Try to delete anyway without error analysis. */
  457. hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  458. goto failed_mknod;
  459. }
  460. hfsplus_instantiate(dentry, inode, inode->i_ino);
  461. mark_inode_dirty(inode);
  462. goto out;
  463. failed_mknod:
  464. clear_nlink(inode);
  465. hfsplus_delete_inode(inode);
  466. iput(inode);
  467. out:
  468. mutex_unlock(&sbi->vh_mutex);
  469. return res;
  470. }
  471. static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  472. bool excl)
  473. {
  474. return hfsplus_mknod(dir, dentry, mode, 0);
  475. }
  476. static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  477. {
  478. return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
  479. }
  480. static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
  481. struct inode *new_dir, struct dentry *new_dentry)
  482. {
  483. int res;
  484. /* Unlink destination if it already exists */
  485. if (d_really_is_positive(new_dentry)) {
  486. if (d_is_dir(new_dentry))
  487. res = hfsplus_rmdir(new_dir, new_dentry);
  488. else
  489. res = hfsplus_unlink(new_dir, new_dentry);
  490. if (res)
  491. return res;
  492. }
  493. res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
  494. old_dir, &old_dentry->d_name,
  495. new_dir, &new_dentry->d_name);
  496. if (!res)
  497. new_dentry->d_fsdata = old_dentry->d_fsdata;
  498. return res;
  499. }
  500. const struct inode_operations hfsplus_dir_inode_operations = {
  501. .lookup = hfsplus_lookup,
  502. .create = hfsplus_create,
  503. .link = hfsplus_link,
  504. .unlink = hfsplus_unlink,
  505. .mkdir = hfsplus_mkdir,
  506. .rmdir = hfsplus_rmdir,
  507. .symlink = hfsplus_symlink,
  508. .mknod = hfsplus_mknod,
  509. .rename = hfsplus_rename,
  510. .setxattr = generic_setxattr,
  511. .getxattr = generic_getxattr,
  512. .listxattr = hfsplus_listxattr,
  513. .removexattr = generic_removexattr,
  514. #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
  515. .get_acl = hfsplus_get_posix_acl,
  516. .set_acl = hfsplus_set_posix_acl,
  517. #endif
  518. };
  519. const struct file_operations hfsplus_dir_operations = {
  520. .fsync = hfsplus_file_fsync,
  521. .read = generic_read_dir,
  522. .iterate = hfsplus_readdir,
  523. .unlocked_ioctl = hfsplus_ioctl,
  524. .llseek = generic_file_llseek,
  525. .release = hfsplus_dir_release,
  526. };