link.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #include "fs.h"
  2. #include <sys/stat.h>
  3. #include <string.h>
  4. #include <minix/com.h>
  5. #include "buf.h"
  6. #include "inode.h"
  7. #include "super.h"
  8. #include <minix/vfsif.h>
  9. #include <sys/param.h>
  10. #define SAME 1000
  11. static int freesp_inode(struct inode *rip, off_t st, off_t end);
  12. static int remove_dir(struct inode *rldirp, struct inode *rip, char
  13. dir_name[MFS_NAME_MAX]);
  14. static int unlink_file(struct inode *dirp, struct inode *rip, char
  15. file_name[MFS_NAME_MAX]);
  16. static off_t nextblock(off_t pos, int zone_size);
  17. static void zerozone_half(struct inode *rip, off_t pos, int half, int
  18. zone_size);
  19. static void zerozone_range(struct inode *rip, off_t pos, off_t len);
  20. /* Args to zerozone_half() */
  21. #define FIRST_HALF 0
  22. #define LAST_HALF 1
  23. /*===========================================================================*
  24. * fs_link *
  25. *===========================================================================*/
  26. int fs_link(ino_t dir_nr, char *name, ino_t ino_nr)
  27. {
  28. /* Perform the link(name1, name2) system call. */
  29. struct inode *ip, *rip;
  30. register int r;
  31. struct inode *new_ip;
  32. /* Temporarily open the file. */
  33. if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
  34. return(EINVAL);
  35. /* Check to see if the file has maximum number of links already. */
  36. r = OK;
  37. if(rip->i_nlinks >= LINK_MAX)
  38. r = EMLINK;
  39. /* Linking to directories is too dangerous to allow. */
  40. if(r == OK)
  41. if( (rip->i_mode & I_TYPE) == I_DIRECTORY)
  42. r = EPERM;
  43. /* If error with 'name', return the inode. */
  44. if (r != OK) {
  45. put_inode(rip);
  46. return(r);
  47. }
  48. /* Temporarily open the last dir */
  49. if( (ip = get_inode(fs_dev, dir_nr)) == NULL) {
  50. put_inode(rip);
  51. return(EINVAL);
  52. }
  53. if (ip->i_nlinks == NO_LINK) { /* Dir does not actually exist */
  54. put_inode(rip);
  55. put_inode(ip);
  56. return(ENOENT);
  57. }
  58. /* If 'name2' exists in full (even if no space) set 'r' to error. */
  59. if((new_ip = advance(ip, name)) == NULL) {
  60. r = err_code;
  61. if(r == ENOENT)
  62. r = OK;
  63. } else {
  64. put_inode(new_ip);
  65. r = EEXIST;
  66. }
  67. /* Try to link. */
  68. if(r == OK)
  69. r = search_dir(ip, name, &rip->i_num, ENTER);
  70. /* If success, register the linking. */
  71. if(r == OK) {
  72. rip->i_nlinks++;
  73. rip->i_update |= CTIME;
  74. IN_MARKDIRTY(rip);
  75. }
  76. /* Done. Release both inodes. */
  77. put_inode(rip);
  78. put_inode(ip);
  79. return(r);
  80. }
  81. /*===========================================================================*
  82. * fs_unlink *
  83. *===========================================================================*/
  84. int fs_unlink(ino_t dir_nr, char *name, int call)
  85. {
  86. /* Perform the unlink(name) or rmdir(name) system call. The code for these two
  87. * is almost the same. They differ only in some condition testing.
  88. */
  89. register struct inode *rip;
  90. struct inode *rldirp;
  91. int r;
  92. /* Temporarily open the dir. */
  93. if((rldirp = get_inode(fs_dev, dir_nr)) == NULL)
  94. return(EINVAL);
  95. /* The last directory exists. Does the file also exist? */
  96. rip = advance(rldirp, name);
  97. r = err_code;
  98. /* If error, return inode. */
  99. if(r != OK) {
  100. put_inode(rldirp);
  101. return(r);
  102. }
  103. if (rip->i_mountpoint) {
  104. put_inode(rip);
  105. put_inode(rldirp);
  106. return(EBUSY);
  107. }
  108. if(rip->i_sp->s_rd_only) {
  109. r = EROFS;
  110. } else if (call == FSC_UNLINK) {
  111. if( (rip->i_mode & I_TYPE) == I_DIRECTORY) r = EPERM;
  112. /* Actually try to unlink the file; fails if parent is mode 0 etc. */
  113. if (r == OK) r = unlink_file(rldirp, rip, name);
  114. } else {
  115. r = remove_dir(rldirp, rip, name); /* call is RMDIR */
  116. }
  117. /* If unlink was possible, it has been done, otherwise it has not. */
  118. put_inode(rip);
  119. put_inode(rldirp);
  120. return(r);
  121. }
  122. /*===========================================================================*
  123. * fs_rdlink *
  124. *===========================================================================*/
  125. ssize_t fs_rdlink(ino_t ino_nr, struct fsdriver_data *data, size_t bytes)
  126. {
  127. struct buf *bp; /* buffer containing link text */
  128. register struct inode *rip; /* target inode */
  129. register int r; /* return value */
  130. /* Temporarily open the file. */
  131. if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
  132. return(EINVAL);
  133. if(!S_ISLNK(rip->i_mode))
  134. r = EACCES;
  135. else {
  136. if(!(bp = get_block_map(rip, 0)))
  137. return EIO;
  138. /* Passed all checks */
  139. if (bytes > rip->i_size)
  140. bytes = rip->i_size;
  141. r = fsdriver_copyout(data, 0, b_data(bp), bytes);
  142. put_block(bp);
  143. if (r == OK)
  144. r = bytes;
  145. }
  146. put_inode(rip);
  147. return(r);
  148. }
  149. /*===========================================================================*
  150. * remove_dir *
  151. *===========================================================================*/
  152. static int remove_dir(rldirp, rip, dir_name)
  153. struct inode *rldirp; /* parent directory */
  154. struct inode *rip; /* directory to be removed */
  155. char dir_name[MFS_NAME_MAX]; /* name of directory to be removed */
  156. {
  157. /* A directory file has to be removed. Five conditions have to met:
  158. * - The file must be a directory
  159. * - The directory must be empty (except for . and ..)
  160. * - The final component of the path must not be . or ..
  161. * - The directory must not be the root of a mounted file system (VFS)
  162. * - The directory must not be anybody's root/working directory (VFS)
  163. */
  164. int r;
  165. /* search_dir checks that rip is a directory too. */
  166. if ((r = search_dir(rip, "", NULL, IS_EMPTY)) != OK)
  167. return(r);
  168. if (rip->i_num == ROOT_INODE) return(EBUSY); /* can't remove 'root' */
  169. /* Actually try to unlink the file; fails if parent is mode 0 etc. */
  170. if ((r = unlink_file(rldirp, rip, dir_name)) != OK) return r;
  171. /* Unlink . and .. from the dir. The super user can link and unlink any dir,
  172. * so don't make too many assumptions about them.
  173. */
  174. (void) unlink_file(rip, NULL, ".");
  175. (void) unlink_file(rip, NULL, "..");
  176. return(OK);
  177. }
  178. /*===========================================================================*
  179. * unlink_file *
  180. *===========================================================================*/
  181. static int unlink_file(dirp, rip, file_name)
  182. struct inode *dirp; /* parent directory of file */
  183. struct inode *rip; /* inode of file, may be NULL too. */
  184. char file_name[MFS_NAME_MAX]; /* name of file to be removed */
  185. {
  186. /* Unlink 'file_name'; rip must be the inode of 'file_name' or NULL. */
  187. ino_t numb; /* inode number */
  188. int r;
  189. /* If rip is not NULL, it is used to get faster access to the inode. */
  190. if (rip == NULL) {
  191. /* Search for file in directory and try to get its inode. */
  192. err_code = search_dir(dirp, file_name, &numb, LOOK_UP);
  193. if (err_code == OK) rip = get_inode(dirp->i_dev, (int) numb);
  194. if (err_code != OK || rip == NULL) return(err_code);
  195. } else {
  196. dup_inode(rip); /* inode will be returned with put_inode */
  197. }
  198. r = search_dir(dirp, file_name, NULL, DELETE);
  199. if (r == OK) {
  200. rip->i_nlinks--; /* entry deleted from parent's dir */
  201. rip->i_update |= CTIME;
  202. IN_MARKDIRTY(rip);
  203. }
  204. put_inode(rip);
  205. return(r);
  206. }
  207. /*===========================================================================*
  208. * fs_rename *
  209. *===========================================================================*/
  210. int fs_rename(ino_t old_dir_nr, char *old_name, ino_t new_dir_nr,
  211. char *new_name)
  212. {
  213. /* Perform the rename(name1, name2) system call. */
  214. struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */
  215. struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */
  216. struct inode *new_superdirp, *next_new_superdirp;
  217. int r = OK; /* error flag; initially no error */
  218. int odir, ndir; /* TRUE iff {old|new} file is dir */
  219. int same_pdir; /* TRUE iff parent dirs are the same */
  220. ino_t numb;
  221. /* Get old dir inode */
  222. if ((old_dirp = get_inode(fs_dev, old_dir_nr)) == NULL)
  223. return(err_code);
  224. old_ip = advance(old_dirp, old_name);
  225. r = err_code;
  226. if (old_ip == NULL) {
  227. put_inode(old_dirp);
  228. return(r);
  229. }
  230. if (old_ip->i_mountpoint) {
  231. put_inode(old_ip);
  232. put_inode(old_dirp);
  233. return(EBUSY);
  234. }
  235. /* Get new dir inode */
  236. if ((new_dirp = get_inode(fs_dev, new_dir_nr)) == NULL) {
  237. put_inode(old_ip);
  238. put_inode(old_dirp);
  239. return(err_code);
  240. } else {
  241. if (new_dirp->i_nlinks == NO_LINK) { /* Dir does not actually exist */
  242. put_inode(old_ip);
  243. put_inode(old_dirp);
  244. put_inode(new_dirp);
  245. return(ENOENT);
  246. }
  247. }
  248. new_ip = advance(new_dirp, new_name); /* not required to exist */
  249. /* If the node does exist, make sure it's not a mountpoint. */
  250. if (new_ip != NULL && new_ip->i_mountpoint) {
  251. put_inode(new_ip);
  252. new_ip = NULL;
  253. r = EBUSY;
  254. }
  255. odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */
  256. /* If it is ok, check for a variety of possible errors. */
  257. if(r == OK) {
  258. same_pdir = (old_dirp == new_dirp);
  259. /* The old inode must not be a superdirectory of the new last dir. */
  260. if (odir && !same_pdir) {
  261. dup_inode(new_superdirp = new_dirp);
  262. while (TRUE) { /* may hang in a file system loop */
  263. if (new_superdirp == old_ip) {
  264. put_inode(new_superdirp);
  265. r = EINVAL;
  266. break;
  267. }
  268. next_new_superdirp = advance(new_superdirp, "..");
  269. put_inode(new_superdirp);
  270. if(next_new_superdirp == new_superdirp) {
  271. put_inode(new_superdirp);
  272. break;
  273. }
  274. if(next_new_superdirp->i_num == ROOT_INODE) {
  275. put_inode(next_new_superdirp);
  276. err_code = OK;
  277. break;
  278. }
  279. new_superdirp = next_new_superdirp;
  280. if(new_superdirp == NULL) {
  281. /* Missing ".." entry. Assume the worst. */
  282. r = EINVAL;
  283. break;
  284. }
  285. }
  286. }
  287. /* Some tests apply only if the new path exists. */
  288. if(new_ip == NULL) {
  289. if (odir && new_dirp->i_nlinks >= LINK_MAX &&
  290. !same_pdir && r == OK) {
  291. r = EMLINK;
  292. }
  293. } else {
  294. if(old_ip == new_ip) r = SAME; /* old=new */
  295. ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY);/* dir ? */
  296. if(odir == TRUE && ndir == FALSE) r = ENOTDIR;
  297. if(odir == FALSE && ndir == TRUE) r = EISDIR;
  298. }
  299. }
  300. /* If a process has another root directory than the system root, we might
  301. * "accidently" be moving it's working directory to a place where it's
  302. * root directory isn't a super directory of it anymore. This can make
  303. * the function chroot useless. If chroot will be used often we should
  304. * probably check for it here. */
  305. /* The rename will probably work. Only two things can go wrong now:
  306. * 1. being unable to remove the new file. (when new file already exists)
  307. * 2. being unable to make the new directory entry. (new file doesn't exists)
  308. * [directory has to grow by one block and cannot because the disk
  309. * is completely full].
  310. */
  311. if(r == OK) {
  312. if(new_ip != NULL) {
  313. /* There is already an entry for 'new'. Try to remove it. */
  314. if(odir)
  315. r = remove_dir(new_dirp, new_ip, new_name);
  316. else
  317. r = unlink_file(new_dirp, new_ip, new_name);
  318. }
  319. /* if r is OK, the rename will succeed, while there is now an
  320. * unused entry in the new parent directory. */
  321. }
  322. if(r == OK) {
  323. /* If the new name will be in the same parent directory as the old
  324. * one, first remove the old name to free an entry for the new name,
  325. * otherwise first try to create the new name entry to make sure
  326. * the rename will succeed.
  327. */
  328. numb = old_ip->i_num; /* inode number of old file */
  329. if(same_pdir) {
  330. r = search_dir(old_dirp, old_name, NULL, DELETE);
  331. /* shouldn't go wrong. */
  332. if(r == OK)
  333. (void) search_dir(old_dirp, new_name, &numb, ENTER);
  334. } else {
  335. r = search_dir(new_dirp, new_name, &numb, ENTER);
  336. if(r == OK)
  337. (void) search_dir(old_dirp, old_name, NULL, DELETE);
  338. }
  339. }
  340. /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
  341. * for update in search_dir. */
  342. if(r == OK && odir && !same_pdir) {
  343. /* Update the .. entry in the directory (still points to old_dirp).*/
  344. numb = new_dirp->i_num;
  345. (void) unlink_file(old_ip, NULL, "..");
  346. if(search_dir(old_ip, "..", &numb, ENTER) == OK) {
  347. /* New link created. */
  348. new_dirp->i_nlinks++;
  349. IN_MARKDIRTY(new_dirp);
  350. }
  351. }
  352. /* Release the inodes. */
  353. put_inode(old_dirp);
  354. put_inode(old_ip);
  355. put_inode(new_dirp);
  356. put_inode(new_ip);
  357. return(r == SAME ? OK : r);
  358. }
  359. /*===========================================================================*
  360. * fs_trunc *
  361. *===========================================================================*/
  362. int fs_trunc(ino_t ino_nr, off_t start, off_t end)
  363. {
  364. struct inode *rip;
  365. int r;
  366. if( (rip = find_inode(fs_dev, ino_nr)) == NULL)
  367. return(EINVAL);
  368. if(rip->i_sp->s_rd_only) {
  369. r = EROFS;
  370. } else {
  371. if (end == 0)
  372. r = truncate_inode(rip, start);
  373. else
  374. r = freesp_inode(rip, start, end);
  375. }
  376. return(r);
  377. }
  378. /*===========================================================================*
  379. * truncate_inode *
  380. *===========================================================================*/
  381. int truncate_inode(rip, newsize)
  382. register struct inode *rip; /* pointer to inode to be truncated */
  383. off_t newsize; /* inode must become this size */
  384. {
  385. /* Set inode to a certain size, freeing any zones no longer referenced
  386. * and updating the size in the inode. If the inode is extended, the
  387. * extra space is a hole that reads as zeroes.
  388. *
  389. * Nothing special has to happen to file pointers if inode is opened in
  390. * O_APPEND mode, as this is different per fd and is checked when
  391. * writing is done.
  392. */
  393. int r;
  394. mode_t file_type;
  395. file_type = rip->i_mode & I_TYPE; /* check to see if file is special */
  396. if (file_type == I_CHAR_SPECIAL || file_type == I_BLOCK_SPECIAL)
  397. return(EINVAL);
  398. if (newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
  399. return(EFBIG);
  400. /* Free the actual space if truncating. */
  401. if (newsize < rip->i_size) {
  402. if ((r = freesp_inode(rip, newsize, rip->i_size)) != OK)
  403. return(r);
  404. }
  405. /* Clear the rest of the last zone if expanding. */
  406. if (newsize > rip->i_size) clear_zone(rip, rip->i_size, 0);
  407. /* Next correct the inode size. */
  408. rip->i_size = newsize;
  409. rip->i_update |= CTIME | MTIME;
  410. IN_MARKDIRTY(rip);
  411. return(OK);
  412. }
  413. /*===========================================================================*
  414. * freesp_inode *
  415. *===========================================================================*/
  416. static int freesp_inode(rip, start, end)
  417. register struct inode *rip; /* pointer to inode to be partly freed */
  418. off_t start, end; /* range of bytes to free (end uninclusive) */
  419. {
  420. /* Cut an arbitrary hole in an inode. The caller is responsible for checking
  421. * the reasonableness of the inode type of rip. The reason is this is that
  422. * this function can be called for different reasons, for which different
  423. * sets of inode types are reasonable. Adjusting the final size of the inode
  424. * is to be done by the caller too, if wished.
  425. *
  426. * Consumers of this function currently are truncate_inode() (used to
  427. * free indirect and data blocks for any type of inode, but also to
  428. * implement the ftruncate() and truncate() system calls) and the F_FREESP
  429. * fcntl().
  430. */
  431. off_t p, e;
  432. int zone_size, r;
  433. int zero_last, zero_first;
  434. if(end > rip->i_size) /* freeing beyond end makes no sense */
  435. end = rip->i_size;
  436. if(end <= start) /* end is uninclusive, so start<end */
  437. return(EINVAL);
  438. zone_size = rip->i_sp->s_block_size << rip->i_sp->s_log_zone_size;
  439. /* If freeing doesn't cross a zone boundary, then we may only zero
  440. * a range of the zone, unless we are freeing up that entire zone.
  441. */
  442. zero_last = start % zone_size;
  443. zero_first = end % zone_size && end < rip->i_size;
  444. if(start/zone_size == (end-1)/zone_size && (zero_last || zero_first)) {
  445. zerozone_range(rip, start, end-start);
  446. } else {
  447. /* First zero unused part of partly used zones. */
  448. if(zero_last)
  449. zerozone_half(rip, start, LAST_HALF, zone_size);
  450. if(zero_first)
  451. zerozone_half(rip, end, FIRST_HALF, zone_size);
  452. /* Now completely free the completely unused zones.
  453. * write_map() will free unused (double) indirect
  454. * blocks too. Converting the range to zone numbers avoids
  455. * overflow on p when doing e.g. 'p += zone_size'.
  456. */
  457. e = end/zone_size;
  458. if(end == rip->i_size && (end % zone_size)) e++;
  459. for(p = nextblock(start, zone_size)/zone_size; p < e; p ++) {
  460. if((r = write_map(rip, p*zone_size, NO_ZONE, WMAP_FREE)) != OK)
  461. return(r);
  462. }
  463. }
  464. rip->i_update |= CTIME | MTIME;
  465. IN_MARKDIRTY(rip);
  466. return(OK);
  467. }
  468. /*===========================================================================*
  469. * nextblock *
  470. *===========================================================================*/
  471. static off_t nextblock(pos, zone_size)
  472. off_t pos;
  473. int zone_size;
  474. {
  475. /* Return the first position in the next block after position 'pos'
  476. * (unless this is the first position in the current block).
  477. * This can be done in one expression, but that can overflow pos.
  478. */
  479. off_t p;
  480. p = (pos/zone_size)*zone_size;
  481. if((pos % zone_size)) p += zone_size; /* Round up. */
  482. return(p);
  483. }
  484. /*===========================================================================*
  485. * zerozone_half *
  486. *===========================================================================*/
  487. static void zerozone_half(rip, pos, half, zone_size)
  488. struct inode *rip;
  489. off_t pos;
  490. int half;
  491. int zone_size;
  492. {
  493. /* Zero the upper or lower 'half' of a zone that holds position 'pos'.
  494. * half can be FIRST_HALF or LAST_HALF.
  495. *
  496. * FIRST_HALF: 0..pos-1 will be zeroed
  497. * LAST_HALF: pos..zone_size-1 will be zeroed
  498. */
  499. off_t offset, len;
  500. /* Offset of zeroing boundary. */
  501. offset = pos % zone_size;
  502. if(half == LAST_HALF) {
  503. len = zone_size - offset;
  504. } else {
  505. len = offset;
  506. pos -= offset;
  507. }
  508. zerozone_range(rip, pos, len);
  509. }
  510. /*===========================================================================*
  511. * zerozone_range *
  512. *===========================================================================*/
  513. static void zerozone_range(rip, pos, len)
  514. struct inode *rip;
  515. off_t pos;
  516. off_t len;
  517. {
  518. /* Zero an arbitrary byte range in a zone, possibly spanning multiple blocks.
  519. */
  520. struct buf *bp;
  521. off_t offset;
  522. unsigned short block_size;
  523. size_t bytes;
  524. block_size = rip->i_sp->s_block_size;
  525. if(!len) return; /* no zeroing to be done. */
  526. while (len > 0) {
  527. if( (bp = get_block_map(rip, rounddown(pos, block_size))) == NULL)
  528. return;
  529. offset = pos % block_size;
  530. bytes = block_size - offset;
  531. if (bytes > (size_t) len)
  532. bytes = len;
  533. memset(b_data(bp) + offset, 0, bytes);
  534. MARKDIRTY(bp);
  535. put_block(bp);
  536. pos += bytes;
  537. len -= bytes;
  538. }
  539. }