dir.c 13 KB

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