dir.c 13 KB

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