namei.c 16 KB

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