namei.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * linux/fs/hpfs/namei.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * adding & removing files & directories
  7. */
  8. #include <linux/sched.h>
  9. #include "hpfs_fn.h"
  10. static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  11. {
  12. const unsigned char *name = dentry->d_name.name;
  13. unsigned len = dentry->d_name.len;
  14. struct quad_buffer_head qbh0;
  15. struct buffer_head *bh;
  16. struct hpfs_dirent *de;
  17. struct fnode *fnode;
  18. struct dnode *dnode;
  19. struct inode *result;
  20. fnode_secno fno;
  21. dnode_secno dno;
  22. int r;
  23. struct hpfs_dirent dee;
  24. int err;
  25. if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
  26. hpfs_lock(dir->i_sb);
  27. err = -ENOSPC;
  28. fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
  29. if (!fnode)
  30. goto bail;
  31. dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
  32. if (!dnode)
  33. goto bail1;
  34. memset(&dee, 0, sizeof dee);
  35. dee.directory = 1;
  36. if (!(mode & 0222)) dee.read_only = 1;
  37. /*dee.archive = 0;*/
  38. dee.hidden = name[0] == '.';
  39. dee.fnode = cpu_to_le32(fno);
  40. dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
  41. result = new_inode(dir->i_sb);
  42. if (!result)
  43. goto bail2;
  44. hpfs_init_inode(result);
  45. result->i_ino = fno;
  46. hpfs_i(result)->i_parent_dir = dir->i_ino;
  47. hpfs_i(result)->i_dno = dno;
  48. result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
  49. result->i_ctime.tv_nsec = 0;
  50. result->i_mtime.tv_nsec = 0;
  51. result->i_atime.tv_nsec = 0;
  52. hpfs_i(result)->i_ea_size = 0;
  53. result->i_mode |= S_IFDIR;
  54. result->i_op = &hpfs_dir_iops;
  55. result->i_fop = &hpfs_dir_ops;
  56. result->i_blocks = 4;
  57. result->i_size = 2048;
  58. set_nlink(result, 2);
  59. if (dee.read_only)
  60. result->i_mode &= ~0222;
  61. r = hpfs_add_dirent(dir, name, len, &dee);
  62. if (r == 1)
  63. goto bail3;
  64. if (r == -1) {
  65. err = -EEXIST;
  66. goto bail3;
  67. }
  68. fnode->len = len;
  69. memcpy(fnode->name, name, len > 15 ? 15 : len);
  70. fnode->up = cpu_to_le32(dir->i_ino);
  71. fnode->flags |= FNODE_dir;
  72. fnode->btree.n_free_nodes = 7;
  73. fnode->btree.n_used_nodes = 1;
  74. fnode->btree.first_free = cpu_to_le16(0x14);
  75. fnode->u.external[0].disk_secno = cpu_to_le32(dno);
  76. fnode->u.external[0].file_secno = cpu_to_le32(-1);
  77. dnode->root_dnode = 1;
  78. dnode->up = cpu_to_le32(fno);
  79. de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
  80. de->creation_date = de->write_date = de->read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
  81. if (!(mode & 0222)) de->read_only = 1;
  82. de->first = de->directory = 1;
  83. /*de->hidden = de->system = 0;*/
  84. de->fnode = cpu_to_le32(fno);
  85. mark_buffer_dirty(bh);
  86. brelse(bh);
  87. hpfs_mark_4buffers_dirty(&qbh0);
  88. hpfs_brelse4(&qbh0);
  89. inc_nlink(dir);
  90. insert_inode_hash(result);
  91. if (!uid_eq(result->i_uid, current_fsuid()) ||
  92. !gid_eq(result->i_gid, current_fsgid()) ||
  93. result->i_mode != (mode | S_IFDIR)) {
  94. result->i_uid = current_fsuid();
  95. result->i_gid = current_fsgid();
  96. result->i_mode = mode | S_IFDIR;
  97. hpfs_write_inode_nolock(result);
  98. }
  99. d_instantiate(dentry, result);
  100. hpfs_unlock(dir->i_sb);
  101. return 0;
  102. bail3:
  103. iput(result);
  104. bail2:
  105. hpfs_brelse4(&qbh0);
  106. hpfs_free_dnode(dir->i_sb, dno);
  107. bail1:
  108. brelse(bh);
  109. hpfs_free_sectors(dir->i_sb, fno, 1);
  110. bail:
  111. hpfs_unlock(dir->i_sb);
  112. return err;
  113. }
  114. static int hpfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  115. {
  116. const unsigned char *name = dentry->d_name.name;
  117. unsigned len = dentry->d_name.len;
  118. struct inode *result = NULL;
  119. struct buffer_head *bh;
  120. struct fnode *fnode;
  121. fnode_secno fno;
  122. int r;
  123. struct hpfs_dirent dee;
  124. int err;
  125. if ((err = hpfs_chk_name(name, &len)))
  126. return err==-ENOENT ? -EINVAL : err;
  127. hpfs_lock(dir->i_sb);
  128. err = -ENOSPC;
  129. fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
  130. if (!fnode)
  131. goto bail;
  132. memset(&dee, 0, sizeof dee);
  133. if (!(mode & 0222)) dee.read_only = 1;
  134. dee.archive = 1;
  135. dee.hidden = name[0] == '.';
  136. dee.fnode = cpu_to_le32(fno);
  137. dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
  138. result = new_inode(dir->i_sb);
  139. if (!result)
  140. goto bail1;
  141. hpfs_init_inode(result);
  142. result->i_ino = fno;
  143. result->i_mode |= S_IFREG;
  144. result->i_mode &= ~0111;
  145. result->i_op = &hpfs_file_iops;
  146. result->i_fop = &hpfs_file_ops;
  147. set_nlink(result, 1);
  148. hpfs_i(result)->i_parent_dir = dir->i_ino;
  149. result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
  150. result->i_ctime.tv_nsec = 0;
  151. result->i_mtime.tv_nsec = 0;
  152. result->i_atime.tv_nsec = 0;
  153. hpfs_i(result)->i_ea_size = 0;
  154. if (dee.read_only)
  155. result->i_mode &= ~0222;
  156. result->i_blocks = 1;
  157. result->i_size = 0;
  158. result->i_data.a_ops = &hpfs_aops;
  159. hpfs_i(result)->mmu_private = 0;
  160. r = hpfs_add_dirent(dir, name, len, &dee);
  161. if (r == 1)
  162. goto bail2;
  163. if (r == -1) {
  164. err = -EEXIST;
  165. goto bail2;
  166. }
  167. fnode->len = len;
  168. memcpy(fnode->name, name, len > 15 ? 15 : len);
  169. fnode->up = cpu_to_le32(dir->i_ino);
  170. mark_buffer_dirty(bh);
  171. brelse(bh);
  172. insert_inode_hash(result);
  173. if (!uid_eq(result->i_uid, current_fsuid()) ||
  174. !gid_eq(result->i_gid, current_fsgid()) ||
  175. result->i_mode != (mode | S_IFREG)) {
  176. result->i_uid = current_fsuid();
  177. result->i_gid = current_fsgid();
  178. result->i_mode = mode | S_IFREG;
  179. hpfs_write_inode_nolock(result);
  180. }
  181. d_instantiate(dentry, result);
  182. hpfs_unlock(dir->i_sb);
  183. return 0;
  184. bail2:
  185. iput(result);
  186. bail1:
  187. brelse(bh);
  188. hpfs_free_sectors(dir->i_sb, fno, 1);
  189. bail:
  190. hpfs_unlock(dir->i_sb);
  191. return err;
  192. }
  193. static int hpfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  194. {
  195. const unsigned char *name = dentry->d_name.name;
  196. unsigned len = dentry->d_name.len;
  197. struct buffer_head *bh;
  198. struct fnode *fnode;
  199. fnode_secno fno;
  200. int r;
  201. struct hpfs_dirent dee;
  202. struct inode *result = NULL;
  203. int err;
  204. if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
  205. if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
  206. if (!new_valid_dev(rdev))
  207. return -EINVAL;
  208. hpfs_lock(dir->i_sb);
  209. err = -ENOSPC;
  210. fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
  211. if (!fnode)
  212. goto bail;
  213. memset(&dee, 0, sizeof dee);
  214. if (!(mode & 0222)) dee.read_only = 1;
  215. dee.archive = 1;
  216. dee.hidden = name[0] == '.';
  217. dee.fnode = cpu_to_le32(fno);
  218. dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
  219. result = new_inode(dir->i_sb);
  220. if (!result)
  221. goto bail1;
  222. hpfs_init_inode(result);
  223. result->i_ino = fno;
  224. hpfs_i(result)->i_parent_dir = dir->i_ino;
  225. result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
  226. result->i_ctime.tv_nsec = 0;
  227. result->i_mtime.tv_nsec = 0;
  228. result->i_atime.tv_nsec = 0;
  229. hpfs_i(result)->i_ea_size = 0;
  230. result->i_uid = current_fsuid();
  231. result->i_gid = current_fsgid();
  232. set_nlink(result, 1);
  233. result->i_size = 0;
  234. result->i_blocks = 1;
  235. init_special_inode(result, mode, rdev);
  236. r = hpfs_add_dirent(dir, name, len, &dee);
  237. if (r == 1)
  238. goto bail2;
  239. if (r == -1) {
  240. err = -EEXIST;
  241. goto bail2;
  242. }
  243. fnode->len = len;
  244. memcpy(fnode->name, name, len > 15 ? 15 : len);
  245. fnode->up = cpu_to_le32(dir->i_ino);
  246. mark_buffer_dirty(bh);
  247. insert_inode_hash(result);
  248. hpfs_write_inode_nolock(result);
  249. d_instantiate(dentry, result);
  250. brelse(bh);
  251. hpfs_unlock(dir->i_sb);
  252. return 0;
  253. bail2:
  254. iput(result);
  255. bail1:
  256. brelse(bh);
  257. hpfs_free_sectors(dir->i_sb, fno, 1);
  258. bail:
  259. hpfs_unlock(dir->i_sb);
  260. return err;
  261. }
  262. static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
  263. {
  264. const unsigned char *name = dentry->d_name.name;
  265. unsigned len = dentry->d_name.len;
  266. struct buffer_head *bh;
  267. struct fnode *fnode;
  268. fnode_secno fno;
  269. int r;
  270. struct hpfs_dirent dee;
  271. struct inode *result;
  272. int err;
  273. if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
  274. hpfs_lock(dir->i_sb);
  275. if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
  276. hpfs_unlock(dir->i_sb);
  277. return -EPERM;
  278. }
  279. err = -ENOSPC;
  280. fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
  281. if (!fnode)
  282. goto bail;
  283. memset(&dee, 0, sizeof dee);
  284. dee.archive = 1;
  285. dee.hidden = name[0] == '.';
  286. dee.fnode = cpu_to_le32(fno);
  287. dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
  288. result = new_inode(dir->i_sb);
  289. if (!result)
  290. goto bail1;
  291. result->i_ino = fno;
  292. hpfs_init_inode(result);
  293. hpfs_i(result)->i_parent_dir = dir->i_ino;
  294. result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
  295. result->i_ctime.tv_nsec = 0;
  296. result->i_mtime.tv_nsec = 0;
  297. result->i_atime.tv_nsec = 0;
  298. hpfs_i(result)->i_ea_size = 0;
  299. result->i_mode = S_IFLNK | 0777;
  300. result->i_uid = current_fsuid();
  301. result->i_gid = current_fsgid();
  302. result->i_blocks = 1;
  303. set_nlink(result, 1);
  304. result->i_size = strlen(symlink);
  305. result->i_op = &page_symlink_inode_operations;
  306. result->i_data.a_ops = &hpfs_symlink_aops;
  307. r = hpfs_add_dirent(dir, name, len, &dee);
  308. if (r == 1)
  309. goto bail2;
  310. if (r == -1) {
  311. err = -EEXIST;
  312. goto bail2;
  313. }
  314. fnode->len = len;
  315. memcpy(fnode->name, name, len > 15 ? 15 : len);
  316. fnode->up = cpu_to_le32(dir->i_ino);
  317. hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
  318. mark_buffer_dirty(bh);
  319. brelse(bh);
  320. insert_inode_hash(result);
  321. hpfs_write_inode_nolock(result);
  322. d_instantiate(dentry, result);
  323. hpfs_unlock(dir->i_sb);
  324. return 0;
  325. bail2:
  326. iput(result);
  327. bail1:
  328. brelse(bh);
  329. hpfs_free_sectors(dir->i_sb, fno, 1);
  330. bail:
  331. hpfs_unlock(dir->i_sb);
  332. return err;
  333. }
  334. static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
  335. {
  336. const unsigned char *name = dentry->d_name.name;
  337. unsigned len = dentry->d_name.len;
  338. struct quad_buffer_head qbh;
  339. struct hpfs_dirent *de;
  340. struct inode *inode = d_inode(dentry);
  341. dnode_secno dno;
  342. int r;
  343. int rep = 0;
  344. int err;
  345. hpfs_lock(dir->i_sb);
  346. hpfs_adjust_length(name, &len);
  347. again:
  348. err = -ENOENT;
  349. de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
  350. if (!de)
  351. goto out;
  352. err = -EPERM;
  353. if (de->first)
  354. goto out1;
  355. err = -EISDIR;
  356. if (de->directory)
  357. goto out1;
  358. r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
  359. switch (r) {
  360. case 1:
  361. hpfs_error(dir->i_sb, "there was error when removing dirent");
  362. err = -EFSERROR;
  363. break;
  364. case 2: /* no space for deleting, try to truncate file */
  365. err = -ENOSPC;
  366. if (rep++)
  367. break;
  368. dentry_unhash(dentry);
  369. if (!d_unhashed(dentry)) {
  370. hpfs_unlock(dir->i_sb);
  371. return -ENOSPC;
  372. }
  373. if (generic_permission(inode, MAY_WRITE) ||
  374. !S_ISREG(inode->i_mode) ||
  375. get_write_access(inode)) {
  376. d_rehash(dentry);
  377. } else {
  378. struct iattr newattrs;
  379. /*pr_info("truncating file before delete.\n");*/
  380. newattrs.ia_size = 0;
  381. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  382. err = notify_change(dentry, &newattrs, NULL);
  383. put_write_access(inode);
  384. if (!err)
  385. goto again;
  386. }
  387. hpfs_unlock(dir->i_sb);
  388. return -ENOSPC;
  389. default:
  390. drop_nlink(inode);
  391. err = 0;
  392. }
  393. goto out;
  394. out1:
  395. hpfs_brelse4(&qbh);
  396. out:
  397. hpfs_unlock(dir->i_sb);
  398. return err;
  399. }
  400. static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
  401. {
  402. const unsigned char *name = dentry->d_name.name;
  403. unsigned len = dentry->d_name.len;
  404. struct quad_buffer_head qbh;
  405. struct hpfs_dirent *de;
  406. struct inode *inode = d_inode(dentry);
  407. dnode_secno dno;
  408. int n_items = 0;
  409. int err;
  410. int r;
  411. hpfs_adjust_length(name, &len);
  412. hpfs_lock(dir->i_sb);
  413. err = -ENOENT;
  414. de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
  415. if (!de)
  416. goto out;
  417. err = -EPERM;
  418. if (de->first)
  419. goto out1;
  420. err = -ENOTDIR;
  421. if (!de->directory)
  422. goto out1;
  423. hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
  424. err = -ENOTEMPTY;
  425. if (n_items)
  426. goto out1;
  427. r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
  428. switch (r) {
  429. case 1:
  430. hpfs_error(dir->i_sb, "there was error when removing dirent");
  431. err = -EFSERROR;
  432. break;
  433. case 2:
  434. err = -ENOSPC;
  435. break;
  436. default:
  437. drop_nlink(dir);
  438. clear_nlink(inode);
  439. err = 0;
  440. }
  441. goto out;
  442. out1:
  443. hpfs_brelse4(&qbh);
  444. out:
  445. hpfs_unlock(dir->i_sb);
  446. return err;
  447. }
  448. static int hpfs_symlink_readpage(struct file *file, struct page *page)
  449. {
  450. char *link = kmap(page);
  451. struct inode *i = page->mapping->host;
  452. struct fnode *fnode;
  453. struct buffer_head *bh;
  454. int err;
  455. err = -EIO;
  456. hpfs_lock(i->i_sb);
  457. if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
  458. goto fail;
  459. err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
  460. brelse(bh);
  461. if (err)
  462. goto fail;
  463. hpfs_unlock(i->i_sb);
  464. SetPageUptodate(page);
  465. kunmap(page);
  466. unlock_page(page);
  467. return 0;
  468. fail:
  469. hpfs_unlock(i->i_sb);
  470. SetPageError(page);
  471. kunmap(page);
  472. unlock_page(page);
  473. return err;
  474. }
  475. const struct address_space_operations hpfs_symlink_aops = {
  476. .readpage = hpfs_symlink_readpage
  477. };
  478. static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  479. struct inode *new_dir, struct dentry *new_dentry)
  480. {
  481. const unsigned char *old_name = old_dentry->d_name.name;
  482. unsigned old_len = old_dentry->d_name.len;
  483. const unsigned char *new_name = new_dentry->d_name.name;
  484. unsigned new_len = new_dentry->d_name.len;
  485. struct inode *i = d_inode(old_dentry);
  486. struct inode *new_inode = d_inode(new_dentry);
  487. struct quad_buffer_head qbh, qbh1;
  488. struct hpfs_dirent *dep, *nde;
  489. struct hpfs_dirent de;
  490. dnode_secno dno;
  491. int r;
  492. struct buffer_head *bh;
  493. struct fnode *fnode;
  494. int err;
  495. if ((err = hpfs_chk_name(new_name, &new_len))) return err;
  496. err = 0;
  497. hpfs_adjust_length(old_name, &old_len);
  498. hpfs_lock(i->i_sb);
  499. /* order doesn't matter, due to VFS exclusion */
  500. /* Erm? Moving over the empty non-busy directory is perfectly legal */
  501. if (new_inode && S_ISDIR(new_inode->i_mode)) {
  502. err = -EINVAL;
  503. goto end1;
  504. }
  505. if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
  506. hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
  507. err = -ENOENT;
  508. goto end1;
  509. }
  510. copy_de(&de, dep);
  511. de.hidden = new_name[0] == '.';
  512. if (new_inode) {
  513. int r;
  514. if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
  515. if ((nde = map_dirent(new_dir, hpfs_i(new_dir)->i_dno, new_name, new_len, NULL, &qbh1))) {
  516. clear_nlink(new_inode);
  517. copy_de(nde, &de);
  518. memcpy(nde->name, new_name, new_len);
  519. hpfs_mark_4buffers_dirty(&qbh1);
  520. hpfs_brelse4(&qbh1);
  521. goto end;
  522. }
  523. hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
  524. err = -EFSERROR;
  525. goto end1;
  526. }
  527. err = r == 2 ? -ENOSPC : r == 1 ? -EFSERROR : 0;
  528. goto end1;
  529. }
  530. if (new_dir == old_dir) hpfs_brelse4(&qbh);
  531. if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
  532. if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
  533. err = r == 1 ? -ENOSPC : -EFSERROR;
  534. if (new_dir != old_dir) hpfs_brelse4(&qbh);
  535. goto end1;
  536. }
  537. if (new_dir == old_dir)
  538. if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
  539. hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
  540. err = -ENOENT;
  541. goto end1;
  542. }
  543. if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
  544. hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
  545. err = r == 2 ? -ENOSPC : -EFSERROR;
  546. goto end1;
  547. }
  548. end:
  549. hpfs_i(i)->i_parent_dir = new_dir->i_ino;
  550. if (S_ISDIR(i->i_mode)) {
  551. inc_nlink(new_dir);
  552. drop_nlink(old_dir);
  553. }
  554. if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
  555. fnode->up = cpu_to_le32(new_dir->i_ino);
  556. fnode->len = new_len;
  557. memcpy(fnode->name, new_name, new_len>15?15:new_len);
  558. if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
  559. mark_buffer_dirty(bh);
  560. brelse(bh);
  561. }
  562. end1:
  563. hpfs_unlock(i->i_sb);
  564. return err;
  565. }
  566. const struct inode_operations hpfs_dir_iops =
  567. {
  568. .create = hpfs_create,
  569. .lookup = hpfs_lookup,
  570. .unlink = hpfs_unlink,
  571. .symlink = hpfs_symlink,
  572. .mkdir = hpfs_mkdir,
  573. .rmdir = hpfs_rmdir,
  574. .mknod = hpfs_mknod,
  575. .rename = hpfs_rename,
  576. .setattr = hpfs_setattr,
  577. };