dir.c 14 KB

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