namei.c 16 KB

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