HardenedBSD src tree https://hardenedbsd.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1942 lines
49 KiB

  1. /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-2-Clause-NetBSD
  4. *
  5. * Copyright (c) 2005 The NetBSD Foundation, Inc.
  6. * All rights reserved.
  7. *
  8. * This code is derived from software contributed to The NetBSD Foundation
  9. * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
  10. * 2005 program.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  23. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  25. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. * Efficient memory file system supporting functions.
  35. */
  36. #include <sys/cdefs.h>
  37. __FBSDID("$FreeBSD$");
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/dirent.h>
  41. #include <sys/fnv_hash.h>
  42. #include <sys/lock.h>
  43. #include <sys/limits.h>
  44. #include <sys/mount.h>
  45. #include <sys/namei.h>
  46. #include <sys/priv.h>
  47. #include <sys/proc.h>
  48. #include <sys/random.h>
  49. #include <sys/rwlock.h>
  50. #include <sys/stat.h>
  51. #include <sys/sysctl.h>
  52. #include <sys/vnode.h>
  53. #include <sys/vmmeter.h>
  54. #include <vm/vm.h>
  55. #include <vm/vm_param.h>
  56. #include <vm/vm_object.h>
  57. #include <vm/vm_page.h>
  58. #include <vm/vm_pageout.h>
  59. #include <vm/vm_pager.h>
  60. #include <vm/vm_extern.h>
  61. #include <vm/swap_pager.h>
  62. #include <fs/tmpfs/tmpfs.h>
  63. #include <fs/tmpfs/tmpfs_fifoops.h>
  64. #include <fs/tmpfs/tmpfs_vnops.h>
  65. SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  66. "tmpfs file system");
  67. static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
  68. static uma_zone_t tmpfs_dirent_pool;
  69. static uma_zone_t tmpfs_node_pool;
  70. static int
  71. tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
  72. {
  73. struct tmpfs_node *node;
  74. node = mem;
  75. node->tn_gen++;
  76. node->tn_size = 0;
  77. node->tn_status = 0;
  78. node->tn_flags = 0;
  79. node->tn_links = 0;
  80. node->tn_vnode = NULL;
  81. node->tn_vpstate = 0;
  82. return (0);
  83. }
  84. static void
  85. tmpfs_node_dtor(void *mem, int size, void *arg)
  86. {
  87. struct tmpfs_node *node;
  88. node = mem;
  89. node->tn_type = VNON;
  90. }
  91. static int
  92. tmpfs_node_init(void *mem, int size, int flags)
  93. {
  94. struct tmpfs_node *node;
  95. node = mem;
  96. node->tn_id = 0;
  97. mtx_init(&node->tn_interlock, "tmpfsni", NULL, MTX_DEF);
  98. node->tn_gen = arc4random();
  99. return (0);
  100. }
  101. static void
  102. tmpfs_node_fini(void *mem, int size)
  103. {
  104. struct tmpfs_node *node;
  105. node = mem;
  106. mtx_destroy(&node->tn_interlock);
  107. }
  108. void
  109. tmpfs_subr_init(void)
  110. {
  111. tmpfs_dirent_pool = uma_zcreate("TMPFS dirent",
  112. sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
  113. UMA_ALIGN_PTR, 0);
  114. tmpfs_node_pool = uma_zcreate("TMPFS node",
  115. sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
  116. tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
  117. }
  118. void
  119. tmpfs_subr_uninit(void)
  120. {
  121. uma_zdestroy(tmpfs_node_pool);
  122. uma_zdestroy(tmpfs_dirent_pool);
  123. }
  124. static int
  125. sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
  126. {
  127. int error;
  128. long pages, bytes;
  129. pages = *(long *)arg1;
  130. bytes = pages * PAGE_SIZE;
  131. error = sysctl_handle_long(oidp, &bytes, 0, req);
  132. if (error || !req->newptr)
  133. return (error);
  134. pages = bytes / PAGE_SIZE;
  135. if (pages < TMPFS_PAGES_MINRESERVED)
  136. return (EINVAL);
  137. *(long *)arg1 = pages;
  138. return (0);
  139. }
  140. SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved,
  141. CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0,
  142. sysctl_mem_reserved, "L",
  143. "Amount of available memory and swap below which tmpfs growth stops");
  144. static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
  145. struct tmpfs_dirent *b);
  146. RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
  147. size_t
  148. tmpfs_mem_avail(void)
  149. {
  150. vm_ooffset_t avail;
  151. avail = swap_pager_avail + vm_free_count() - tmpfs_pages_reserved;
  152. if (__predict_false(avail < 0))
  153. avail = 0;
  154. return (avail);
  155. }
  156. size_t
  157. tmpfs_pages_used(struct tmpfs_mount *tmp)
  158. {
  159. const size_t node_size = sizeof(struct tmpfs_node) +
  160. sizeof(struct tmpfs_dirent);
  161. size_t meta_pages;
  162. meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
  163. PAGE_SIZE);
  164. return (meta_pages + tmp->tm_pages_used);
  165. }
  166. static size_t
  167. tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
  168. {
  169. if (tmpfs_mem_avail() < req_pages)
  170. return (0);
  171. if (tmp->tm_pages_max != ULONG_MAX &&
  172. tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
  173. return (0);
  174. return (1);
  175. }
  176. void
  177. tmpfs_ref_node(struct tmpfs_node *node)
  178. {
  179. TMPFS_NODE_LOCK(node);
  180. tmpfs_ref_node_locked(node);
  181. TMPFS_NODE_UNLOCK(node);
  182. }
  183. void
  184. tmpfs_ref_node_locked(struct tmpfs_node *node)
  185. {
  186. TMPFS_NODE_ASSERT_LOCKED(node);
  187. KASSERT(node->tn_refcount > 0, ("node %p zero refcount", node));
  188. KASSERT(node->tn_refcount < UINT_MAX, ("node %p refcount %u", node,
  189. node->tn_refcount));
  190. node->tn_refcount++;
  191. }
  192. /*
  193. * Allocates a new node of type 'type' inside the 'tmp' mount point, with
  194. * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
  195. * using the credentials of the process 'p'.
  196. *
  197. * If the node type is set to 'VDIR', then the parent parameter must point
  198. * to the parent directory of the node being created. It may only be NULL
  199. * while allocating the root node.
  200. *
  201. * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
  202. * specifies the device the node represents.
  203. *
  204. * If the node type is set to 'VLNK', then the parameter target specifies
  205. * the file name of the target file for the symbolic link that is being
  206. * created.
  207. *
  208. * Note that new nodes are retrieved from the available list if it has
  209. * items or, if it is empty, from the node pool as long as there is enough
  210. * space to create them.
  211. *
  212. * Returns zero on success or an appropriate error code on failure.
  213. */
  214. int
  215. tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
  216. uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
  217. const char *target, dev_t rdev, struct tmpfs_node **node)
  218. {
  219. struct tmpfs_node *nnode;
  220. vm_object_t obj;
  221. /* If the root directory of the 'tmp' file system is not yet
  222. * allocated, this must be the request to do it. */
  223. MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
  224. MPASS(IFF(type == VLNK, target != NULL));
  225. MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
  226. if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
  227. return (ENOSPC);
  228. if (tmpfs_pages_check_avail(tmp, 1) == 0)
  229. return (ENOSPC);
  230. if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
  231. /*
  232. * When a new tmpfs node is created for fully
  233. * constructed mount point, there must be a parent
  234. * node, which vnode is locked exclusively. As
  235. * consequence, if the unmount is executing in
  236. * parallel, vflush() cannot reclaim the parent vnode.
  237. * Due to this, the check for MNTK_UNMOUNT flag is not
  238. * racy: if we did not see MNTK_UNMOUNT flag, then tmp
  239. * cannot be destroyed until node construction is
  240. * finished and the parent vnode unlocked.
  241. *
  242. * Tmpfs does not need to instantiate new nodes during
  243. * unmount.
  244. */
  245. return (EBUSY);
  246. }
  247. if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
  248. return (EROFS);
  249. nnode = uma_zalloc_arg(tmpfs_node_pool, tmp, M_WAITOK);
  250. /* Generic initialization. */
  251. nnode->tn_type = type;
  252. vfs_timestamp(&nnode->tn_atime);
  253. nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
  254. nnode->tn_atime;
  255. nnode->tn_uid = uid;
  256. nnode->tn_gid = gid;
  257. nnode->tn_mode = mode;
  258. nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
  259. nnode->tn_refcount = 1;
  260. /* Type-specific initialization. */
  261. switch (nnode->tn_type) {
  262. case VBLK:
  263. case VCHR:
  264. nnode->tn_rdev = rdev;
  265. break;
  266. case VDIR:
  267. RB_INIT(&nnode->tn_dir.tn_dirhead);
  268. LIST_INIT(&nnode->tn_dir.tn_dupindex);
  269. MPASS(parent != nnode);
  270. MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
  271. nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
  272. nnode->tn_dir.tn_readdir_lastn = 0;
  273. nnode->tn_dir.tn_readdir_lastp = NULL;
  274. nnode->tn_links++;
  275. TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
  276. nnode->tn_dir.tn_parent->tn_links++;
  277. TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
  278. break;
  279. case VFIFO:
  280. /* FALLTHROUGH */
  281. case VSOCK:
  282. break;
  283. case VLNK:
  284. MPASS(strlen(target) < MAXPATHLEN);
  285. nnode->tn_size = strlen(target);
  286. nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
  287. M_WAITOK);
  288. memcpy(nnode->tn_link, target, nnode->tn_size);
  289. break;
  290. case VREG:
  291. LIST_INIT(&(nnode->tn_reg.tn_extattr_list));
  292. obj = nnode->tn_reg.tn_aobj =
  293. vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
  294. NULL /* XXXKIB - tmpfs needs swap reservation */);
  295. VM_OBJECT_WLOCK(obj);
  296. /* OBJ_TMPFS is set together with the setting of vp->v_object */
  297. vm_object_set_flag(obj, OBJ_TMPFS_NODE);
  298. VM_OBJECT_WUNLOCK(obj);
  299. break;
  300. default:
  301. panic("tmpfs_alloc_node: type %p %d", nnode,
  302. (int)nnode->tn_type);
  303. }
  304. TMPFS_LOCK(tmp);
  305. LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
  306. nnode->tn_attached = true;
  307. tmp->tm_nodes_inuse++;
  308. tmp->tm_refcount++;
  309. TMPFS_UNLOCK(tmp);
  310. *node = nnode;
  311. return (0);
  312. }
  313. /*
  314. * Destroys the node pointed to by node from the file system 'tmp'.
  315. * If the node references a directory, no entries are allowed.
  316. */
  317. void
  318. tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
  319. {
  320. TMPFS_LOCK(tmp);
  321. TMPFS_NODE_LOCK(node);
  322. if (!tmpfs_free_node_locked(tmp, node, false)) {
  323. TMPFS_NODE_UNLOCK(node);
  324. TMPFS_UNLOCK(tmp);
  325. }
  326. }
  327. bool
  328. tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
  329. bool detach)
  330. {
  331. struct tmpfs_extattr_list_entry *attr, *tattr;
  332. vm_object_t uobj;
  333. TMPFS_MP_ASSERT_LOCKED(tmp);
  334. TMPFS_NODE_ASSERT_LOCKED(node);
  335. KASSERT(node->tn_refcount > 0, ("node %p refcount zero", node));
  336. node->tn_refcount--;
  337. if (node->tn_attached && (detach || node->tn_refcount == 0)) {
  338. MPASS(tmp->tm_nodes_inuse > 0);
  339. tmp->tm_nodes_inuse--;
  340. LIST_REMOVE(node, tn_entries);
  341. node->tn_attached = false;
  342. }
  343. if (node->tn_refcount > 0)
  344. return (false);
  345. #ifdef INVARIANTS
  346. MPASS(node->tn_vnode == NULL);
  347. MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
  348. #endif
  349. TMPFS_NODE_UNLOCK(node);
  350. TMPFS_UNLOCK(tmp);
  351. switch (node->tn_type) {
  352. case VBLK:
  353. /* FALLTHROUGH */
  354. case VCHR:
  355. /* FALLTHROUGH */
  356. case VDIR:
  357. /* FALLTHROUGH */
  358. case VFIFO:
  359. /* FALLTHROUGH */
  360. case VSOCK:
  361. break;
  362. case VLNK:
  363. free(node->tn_link, M_TMPFSNAME);
  364. break;
  365. case VREG:
  366. LIST_FOREACH_SAFE(attr, &(node->tn_reg.tn_extattr_list),
  367. tele_entries, tattr) {
  368. free(attr->tele_value, M_TEMP);
  369. free(attr, M_TEMP);
  370. }
  371. uobj = node->tn_reg.tn_aobj;
  372. if (uobj != NULL) {
  373. if (uobj->size != 0)
  374. atomic_subtract_long(&tmp->tm_pages_used, uobj->size);
  375. KASSERT((uobj->flags & OBJ_TMPFS) == 0,
  376. ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
  377. vm_object_deallocate(uobj);
  378. }
  379. break;
  380. default:
  381. panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
  382. }
  383. uma_zfree(tmpfs_node_pool, node);
  384. TMPFS_LOCK(tmp);
  385. tmpfs_free_tmp(tmp);
  386. return (true);
  387. }
  388. static __inline uint32_t
  389. tmpfs_dirent_hash(const char *name, u_int len)
  390. {
  391. uint32_t hash;
  392. hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
  393. #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
  394. hash &= 0xf;
  395. #endif
  396. if (hash < TMPFS_DIRCOOKIE_MIN)
  397. hash += TMPFS_DIRCOOKIE_MIN;
  398. return (hash);
  399. }
  400. static __inline off_t
  401. tmpfs_dirent_cookie(struct tmpfs_dirent *de)
  402. {
  403. if (de == NULL)
  404. return (TMPFS_DIRCOOKIE_EOF);
  405. MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
  406. return (de->td_cookie);
  407. }
  408. static __inline boolean_t
  409. tmpfs_dirent_dup(struct tmpfs_dirent *de)
  410. {
  411. return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
  412. }
  413. static __inline boolean_t
  414. tmpfs_dirent_duphead(struct tmpfs_dirent *de)
  415. {
  416. return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
  417. }
  418. void
  419. tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
  420. {
  421. de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
  422. memcpy(de->ud.td_name, name, namelen);
  423. de->td_namelen = namelen;
  424. }
  425. /*
  426. * Allocates a new directory entry for the node node with a name of name.
  427. * The new directory entry is returned in *de.
  428. *
  429. * The link count of node is increased by one to reflect the new object
  430. * referencing it.
  431. *
  432. * Returns zero on success or an appropriate error code on failure.
  433. */
  434. int
  435. tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
  436. const char *name, u_int len, struct tmpfs_dirent **de)
  437. {
  438. struct tmpfs_dirent *nde;
  439. nde = uma_zalloc(tmpfs_dirent_pool, M_WAITOK);
  440. nde->td_node = node;
  441. if (name != NULL) {
  442. nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
  443. tmpfs_dirent_init(nde, name, len);
  444. } else
  445. nde->td_namelen = 0;
  446. if (node != NULL)
  447. node->tn_links++;
  448. *de = nde;
  449. return 0;
  450. }
  451. /*
  452. * Frees a directory entry. It is the caller's responsibility to destroy
  453. * the node referenced by it if needed.
  454. *
  455. * The link count of node is decreased by one to reflect the removal of an
  456. * object that referenced it. This only happens if 'node_exists' is true;
  457. * otherwise the function will not access the node referred to by the
  458. * directory entry, as it may already have been released from the outside.
  459. */
  460. void
  461. tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
  462. {
  463. struct tmpfs_node *node;
  464. node = de->td_node;
  465. if (node != NULL) {
  466. MPASS(node->tn_links > 0);
  467. node->tn_links--;
  468. }
  469. if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
  470. free(de->ud.td_name, M_TMPFSNAME);
  471. uma_zfree(tmpfs_dirent_pool, de);
  472. }
  473. void
  474. tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
  475. {
  476. ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
  477. if (vp->v_type != VREG || obj == NULL)
  478. return;
  479. VM_OBJECT_WLOCK(obj);
  480. VI_LOCK(vp);
  481. vm_object_clear_flag(obj, OBJ_TMPFS);
  482. obj->un_pager.swp.swp_tmpfs = NULL;
  483. if (vp->v_writecount < 0)
  484. vp->v_writecount = 0;
  485. VI_UNLOCK(vp);
  486. VM_OBJECT_WUNLOCK(obj);
  487. }
  488. /*
  489. * Need to clear v_object for insmntque failure.
  490. */
  491. static void
  492. tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
  493. {
  494. tmpfs_destroy_vobject(vp, vp->v_object);
  495. vp->v_object = NULL;
  496. vp->v_data = NULL;
  497. vp->v_op = &dead_vnodeops;
  498. vgone(vp);
  499. vput(vp);
  500. }
  501. /*
  502. * Allocates a new vnode for the node node or returns a new reference to
  503. * an existing one if the node had already a vnode referencing it. The
  504. * resulting locked vnode is returned in *vpp.
  505. *
  506. * Returns zero on success or an appropriate error code on failure.
  507. */
  508. int
  509. tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
  510. struct vnode **vpp)
  511. {
  512. struct vnode *vp;
  513. struct tmpfs_mount *tm;
  514. vm_object_t object;
  515. int error;
  516. error = 0;
  517. tm = VFS_TO_TMPFS(mp);
  518. TMPFS_NODE_LOCK(node);
  519. tmpfs_ref_node_locked(node);
  520. loop:
  521. TMPFS_NODE_ASSERT_LOCKED(node);
  522. if ((vp = node->tn_vnode) != NULL) {
  523. MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
  524. VI_LOCK(vp);
  525. if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
  526. (VN_IS_DOOMED(vp) &&
  527. (lkflag & LK_NOWAIT) != 0)) {
  528. VI_UNLOCK(vp);
  529. TMPFS_NODE_UNLOCK(node);
  530. error = ENOENT;
  531. vp = NULL;
  532. goto out;
  533. }
  534. if (VN_IS_DOOMED(vp)) {
  535. VI_UNLOCK(vp);
  536. node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
  537. while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
  538. msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
  539. 0, "tmpfsE", 0);
  540. }
  541. goto loop;
  542. }
  543. TMPFS_NODE_UNLOCK(node);
  544. error = vget(vp, lkflag | LK_INTERLOCK, curthread);
  545. if (error == ENOENT) {
  546. TMPFS_NODE_LOCK(node);
  547. goto loop;
  548. }
  549. if (error != 0) {
  550. vp = NULL;
  551. goto out;
  552. }
  553. /*
  554. * Make sure the vnode is still there after
  555. * getting the interlock to avoid racing a free.
  556. */
  557. if (node->tn_vnode == NULL || node->tn_vnode != vp) {
  558. vput(vp);
  559. TMPFS_NODE_LOCK(node);
  560. goto loop;
  561. }
  562. goto out;
  563. }
  564. if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
  565. (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
  566. TMPFS_NODE_UNLOCK(node);
  567. error = ENOENT;
  568. vp = NULL;
  569. goto out;
  570. }
  571. /*
  572. * otherwise lock the vp list while we call getnewvnode
  573. * since that can block.
  574. */
  575. if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
  576. node->tn_vpstate |= TMPFS_VNODE_WANT;
  577. error = msleep((caddr_t) &node->tn_vpstate,
  578. TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
  579. if (error != 0)
  580. goto out;
  581. goto loop;
  582. } else
  583. node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
  584. TMPFS_NODE_UNLOCK(node);
  585. /* Get a new vnode and associate it with our node. */
  586. error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
  587. &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
  588. if (error != 0)
  589. goto unlock;
  590. MPASS(vp != NULL);
  591. /* lkflag is ignored, the lock is exclusive */
  592. (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  593. vp->v_data = node;
  594. vp->v_type = node->tn_type;
  595. /* Type-specific initialization. */
  596. switch (node->tn_type) {
  597. case VBLK:
  598. /* FALLTHROUGH */
  599. case VCHR:
  600. /* FALLTHROUGH */
  601. case VLNK:
  602. /* FALLTHROUGH */
  603. case VSOCK:
  604. break;
  605. case VFIFO:
  606. vp->v_op = &tmpfs_fifoop_entries;
  607. break;
  608. case VREG:
  609. object = node->tn_reg.tn_aobj;
  610. VM_OBJECT_WLOCK(object);
  611. VI_LOCK(vp);
  612. KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
  613. vp->v_object = object;
  614. object->un_pager.swp.swp_tmpfs = vp;
  615. vm_object_set_flag(object, OBJ_TMPFS);
  616. VI_UNLOCK(vp);
  617. VM_OBJECT_WUNLOCK(object);
  618. break;
  619. case VDIR:
  620. MPASS(node->tn_dir.tn_parent != NULL);
  621. if (node->tn_dir.tn_parent == node)
  622. vp->v_vflag |= VV_ROOT;
  623. break;
  624. default:
  625. panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
  626. }
  627. if (vp->v_type != VFIFO)
  628. VN_LOCK_ASHARE(vp);
  629. error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
  630. if (error != 0)
  631. vp = NULL;
  632. unlock:
  633. TMPFS_NODE_LOCK(node);
  634. MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
  635. node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
  636. node->tn_vnode = vp;
  637. if (node->tn_vpstate & TMPFS_VNODE_WANT) {
  638. node->tn_vpstate &= ~TMPFS_VNODE_WANT;
  639. TMPFS_NODE_UNLOCK(node);
  640. wakeup((caddr_t) &node->tn_vpstate);
  641. } else
  642. TMPFS_NODE_UNLOCK(node);
  643. out:
  644. if (error == 0) {
  645. *vpp = vp;
  646. #ifdef INVARIANTS
  647. MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
  648. TMPFS_NODE_LOCK(node);
  649. MPASS(*vpp == node->tn_vnode);
  650. TMPFS_NODE_UNLOCK(node);
  651. #endif
  652. }
  653. tmpfs_free_node(tm, node);
  654. return (error);
  655. }
  656. /*
  657. * Destroys the association between the vnode vp and the node it
  658. * references.
  659. */
  660. void
  661. tmpfs_free_vp(struct vnode *vp)
  662. {
  663. struct tmpfs_node *node;
  664. node = VP_TO_TMPFS_NODE(vp);
  665. TMPFS_NODE_ASSERT_LOCKED(node);
  666. node->tn_vnode = NULL;
  667. if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
  668. wakeup(&node->tn_vnode);
  669. node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
  670. vp->v_data = NULL;
  671. }
  672. /*
  673. * Allocates a new file of type 'type' and adds it to the parent directory
  674. * 'dvp'; this addition is done using the component name given in 'cnp'.
  675. * The ownership of the new file is automatically assigned based on the
  676. * credentials of the caller (through 'cnp'), the group is set based on
  677. * the parent directory and the mode is determined from the 'vap' argument.
  678. * If successful, *vpp holds a vnode to the newly created file and zero
  679. * is returned. Otherwise *vpp is NULL and the function returns an
  680. * appropriate error code.
  681. */
  682. int
  683. tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
  684. struct componentname *cnp, const char *target)
  685. {
  686. int error;
  687. struct tmpfs_dirent *de;
  688. struct tmpfs_mount *tmp;
  689. struct tmpfs_node *dnode;
  690. struct tmpfs_node *node;
  691. struct tmpfs_node *parent;
  692. ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
  693. MPASS(cnp->cn_flags & HASBUF);
  694. tmp = VFS_TO_TMPFS(dvp->v_mount);
  695. dnode = VP_TO_TMPFS_DIR(dvp);
  696. *vpp = NULL;
  697. /* If the entry we are creating is a directory, we cannot overflow
  698. * the number of links of its parent, because it will get a new
  699. * link. */
  700. if (vap->va_type == VDIR) {
  701. /* Ensure that we do not overflow the maximum number of links
  702. * imposed by the system. */
  703. MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
  704. if (dnode->tn_links == TMPFS_LINK_MAX) {
  705. return (EMLINK);
  706. }
  707. parent = dnode;
  708. MPASS(parent != NULL);
  709. } else
  710. parent = NULL;
  711. /* Allocate a node that represents the new file. */
  712. error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
  713. cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
  714. target, vap->va_rdev, &node);
  715. if (error != 0)
  716. return (error);
  717. /* Allocate a directory entry that points to the new file. */
  718. error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
  719. &de);
  720. if (error != 0) {
  721. tmpfs_free_node(tmp, node);
  722. return (error);
  723. }
  724. /* Allocate a vnode for the new file. */
  725. error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
  726. if (error != 0) {
  727. tmpfs_free_dirent(tmp, de);
  728. tmpfs_free_node(tmp, node);
  729. return (error);
  730. }
  731. /* Now that all required items are allocated, we can proceed to
  732. * insert the new node into the directory, an operation that
  733. * cannot fail. */
  734. if (cnp->cn_flags & ISWHITEOUT)
  735. tmpfs_dir_whiteout_remove(dvp, cnp);
  736. tmpfs_dir_attach(dvp, de);
  737. return (0);
  738. }
  739. struct tmpfs_dirent *
  740. tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
  741. {
  742. struct tmpfs_dirent *de;
  743. de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
  744. dc->tdc_tree = de;
  745. if (de != NULL && tmpfs_dirent_duphead(de))
  746. de = LIST_FIRST(&de->ud.td_duphead);
  747. dc->tdc_current = de;
  748. return (dc->tdc_current);
  749. }
  750. struct tmpfs_dirent *
  751. tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
  752. {
  753. struct tmpfs_dirent *de;
  754. MPASS(dc->tdc_tree != NULL);
  755. if (tmpfs_dirent_dup(dc->tdc_current)) {
  756. dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
  757. if (dc->tdc_current != NULL)
  758. return (dc->tdc_current);
  759. }
  760. dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
  761. &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
  762. if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
  763. dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
  764. MPASS(dc->tdc_current != NULL);
  765. }
  766. return (dc->tdc_current);
  767. }
  768. /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
  769. static struct tmpfs_dirent *
  770. tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
  771. {
  772. struct tmpfs_dirent *de, dekey;
  773. dekey.td_hash = hash;
  774. de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
  775. return (de);
  776. }
  777. /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
  778. static struct tmpfs_dirent *
  779. tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
  780. struct tmpfs_dir_cursor *dc)
  781. {
  782. struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
  783. struct tmpfs_dirent *de, dekey;
  784. MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
  785. if (cookie == node->tn_dir.tn_readdir_lastn &&
  786. (de = node->tn_dir.tn_readdir_lastp) != NULL) {
  787. /* Protect against possible race, tn_readdir_last[pn]
  788. * may be updated with only shared vnode lock held. */
  789. if (cookie == tmpfs_dirent_cookie(de))
  790. goto out;
  791. }
  792. if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
  793. LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
  794. uh.td_dup.index_entries) {
  795. MPASS(tmpfs_dirent_dup(de));
  796. if (de->td_cookie == cookie)
  797. goto out;
  798. /* dupindex list is sorted. */
  799. if (de->td_cookie < cookie) {
  800. de = NULL;
  801. goto out;
  802. }
  803. }
  804. MPASS(de == NULL);
  805. goto out;
  806. }
  807. if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
  808. de = NULL;
  809. } else {
  810. dekey.td_hash = cookie;
  811. /* Recover if direntry for cookie was removed */
  812. de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
  813. }
  814. dc->tdc_tree = de;
  815. dc->tdc_current = de;
  816. if (de != NULL && tmpfs_dirent_duphead(de)) {
  817. dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
  818. MPASS(dc->tdc_current != NULL);
  819. }
  820. return (dc->tdc_current);
  821. out:
  822. dc->tdc_tree = de;
  823. dc->tdc_current = de;
  824. if (de != NULL && tmpfs_dirent_dup(de))
  825. dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
  826. de->td_hash);
  827. return (dc->tdc_current);
  828. }
  829. /*
  830. * Looks for a directory entry in the directory represented by node.
  831. * 'cnp' describes the name of the entry to look for. Note that the .
  832. * and .. components are not allowed as they do not physically exist
  833. * within directories.
  834. *
  835. * Returns a pointer to the entry when found, otherwise NULL.
  836. */
  837. struct tmpfs_dirent *
  838. tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
  839. struct componentname *cnp)
  840. {
  841. struct tmpfs_dir_duphead *duphead;
  842. struct tmpfs_dirent *de;
  843. uint32_t hash;
  844. MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
  845. MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
  846. cnp->cn_nameptr[1] == '.')));
  847. TMPFS_VALIDATE_DIR(node);
  848. hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
  849. de = tmpfs_dir_xlookup_hash(node, hash);
  850. if (de != NULL && tmpfs_dirent_duphead(de)) {
  851. duphead = &de->ud.td_duphead;
  852. LIST_FOREACH(de, duphead, uh.td_dup.entries) {
  853. if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
  854. cnp->cn_namelen))
  855. break;
  856. }
  857. } else if (de != NULL) {
  858. if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
  859. cnp->cn_namelen))
  860. de = NULL;
  861. }
  862. if (de != NULL && f != NULL && de->td_node != f)
  863. de = NULL;
  864. return (de);
  865. }
  866. /*
  867. * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
  868. * list, allocate new cookie value.
  869. */
  870. static void
  871. tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
  872. struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
  873. {
  874. struct tmpfs_dir_duphead *dupindex;
  875. struct tmpfs_dirent *de, *pde;
  876. dupindex = &dnode->tn_dir.tn_dupindex;
  877. de = LIST_FIRST(dupindex);
  878. if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
  879. if (de == NULL)
  880. nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
  881. else
  882. nde->td_cookie = de->td_cookie + 1;
  883. MPASS(tmpfs_dirent_dup(nde));
  884. LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
  885. LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
  886. return;
  887. }
  888. /*
  889. * Cookie numbers are near exhaustion. Scan dupindex list for unused
  890. * numbers. dupindex list is sorted in descending order. Keep it so
  891. * after inserting nde.
  892. */
  893. while (1) {
  894. pde = de;
  895. de = LIST_NEXT(de, uh.td_dup.index_entries);
  896. if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
  897. /*
  898. * Last element of the index doesn't have minimal cookie
  899. * value, use it.
  900. */
  901. nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
  902. LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
  903. LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
  904. return;
  905. } else if (de == NULL) {
  906. /*
  907. * We are so lucky have 2^30 hash duplicates in single
  908. * directory :) Return largest possible cookie value.
  909. * It should be fine except possible issues with
  910. * VOP_READDIR restart.
  911. */
  912. nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
  913. LIST_INSERT_HEAD(dupindex, nde,
  914. uh.td_dup.index_entries);
  915. LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
  916. return;
  917. }
  918. if (de->td_cookie + 1 == pde->td_cookie ||
  919. de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
  920. continue; /* No hole or invalid cookie. */
  921. nde->td_cookie = de->td_cookie + 1;
  922. MPASS(tmpfs_dirent_dup(nde));
  923. MPASS(pde->td_cookie > nde->td_cookie);
  924. MPASS(nde->td_cookie > de->td_cookie);
  925. LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
  926. LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
  927. return;
  928. }
  929. }
  930. /*
  931. * Attaches the directory entry de to the directory represented by vp.
  932. * Note that this does not change the link count of the node pointed by
  933. * the directory entry, as this is done by tmpfs_alloc_dirent.
  934. */
  935. void
  936. tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
  937. {
  938. struct tmpfs_node *dnode;
  939. struct tmpfs_dirent *xde, *nde;
  940. ASSERT_VOP_ELOCKED(vp, __func__);
  941. MPASS(de->td_namelen > 0);
  942. MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
  943. MPASS(de->td_cookie == de->td_hash);
  944. dnode = VP_TO_TMPFS_DIR(vp);
  945. dnode->tn_dir.tn_readdir_lastn = 0;
  946. dnode->tn_dir.tn_readdir_lastp = NULL;
  947. MPASS(!tmpfs_dirent_dup(de));
  948. xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
  949. if (xde != NULL && tmpfs_dirent_duphead(xde))
  950. tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
  951. else if (xde != NULL) {
  952. /*
  953. * Allocate new duphead. Swap xde with duphead to avoid
  954. * adding/removing elements with the same hash.
  955. */
  956. MPASS(!tmpfs_dirent_dup(xde));
  957. tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
  958. &nde);
  959. /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
  960. memcpy(nde, xde, sizeof(*xde));
  961. xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
  962. LIST_INIT(&xde->ud.td_duphead);
  963. xde->td_namelen = 0;
  964. xde->td_node = NULL;
  965. tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
  966. tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
  967. }
  968. dnode->tn_size += sizeof(struct tmpfs_dirent);
  969. dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
  970. TMPFS_NODE_MODIFIED;
  971. tmpfs_update(vp);
  972. }
  973. /*
  974. * Detaches the directory entry de from the directory represented by vp.
  975. * Note that this does not change the link count of the node pointed by
  976. * the directory entry, as this is done by tmpfs_free_dirent.
  977. */
  978. void
  979. tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
  980. {
  981. struct tmpfs_mount *tmp;
  982. struct tmpfs_dir *head;
  983. struct tmpfs_node *dnode;
  984. struct tmpfs_dirent *xde;
  985. ASSERT_VOP_ELOCKED(vp, __func__);
  986. dnode = VP_TO_TMPFS_DIR(vp);
  987. head = &dnode->tn_dir.tn_dirhead;
  988. dnode->tn_dir.tn_readdir_lastn = 0;
  989. dnode->tn_dir.tn_readdir_lastp = NULL;
  990. if (tmpfs_dirent_dup(de)) {
  991. /* Remove duphead if de was last entry. */
  992. if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
  993. xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
  994. MPASS(tmpfs_dirent_duphead(xde));
  995. } else
  996. xde = NULL;
  997. LIST_REMOVE(de, uh.td_dup.entries);
  998. LIST_REMOVE(de, uh.td_dup.index_entries);
  999. if (xde != NULL) {
  1000. if (LIST_EMPTY(&xde->ud.td_duphead)) {
  1001. RB_REMOVE(tmpfs_dir, head, xde);
  1002. tmp = VFS_TO_TMPFS(vp->v_mount);
  1003. MPASS(xde->td_node == NULL);
  1004. tmpfs_free_dirent(tmp, xde);
  1005. }
  1006. }
  1007. de->td_cookie = de->td_hash;
  1008. } else
  1009. RB_REMOVE(tmpfs_dir, head, de);
  1010. dnode->tn_size -= sizeof(struct tmpfs_dirent);
  1011. dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
  1012. TMPFS_NODE_MODIFIED;
  1013. tmpfs_update(vp);
  1014. }
  1015. void
  1016. tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
  1017. {
  1018. struct tmpfs_dirent *de, *dde, *nde;
  1019. RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
  1020. RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
  1021. /* Node may already be destroyed. */
  1022. de->td_node = NULL;
  1023. if (tmpfs_dirent_duphead(de)) {
  1024. while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
  1025. LIST_REMOVE(dde, uh.td_dup.entries);
  1026. dde->td_node = NULL;
  1027. tmpfs_free_dirent(tmp, dde);
  1028. }
  1029. }
  1030. tmpfs_free_dirent(tmp, de);
  1031. }
  1032. }
  1033. /*
  1034. * Helper function for tmpfs_readdir. Creates a '.' entry for the given
  1035. * directory and returns it in the uio space. The function returns 0
  1036. * on success, -1 if there was not enough space in the uio structure to
  1037. * hold the directory entry or an appropriate error code if another
  1038. * error happens.
  1039. */
  1040. static int
  1041. tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
  1042. struct uio *uio)
  1043. {
  1044. int error;
  1045. struct dirent dent;
  1046. TMPFS_VALIDATE_DIR(node);
  1047. MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
  1048. dent.d_fileno = node->tn_id;
  1049. dent.d_type = DT_DIR;
  1050. dent.d_namlen = 1;
  1051. dent.d_name[0] = '.';
  1052. dent.d_reclen = GENERIC_DIRSIZ(&dent);
  1053. dirent_terminate(&dent);
  1054. if (dent.d_reclen > uio->uio_resid)
  1055. error = EJUSTRETURN;
  1056. else
  1057. error = uiomove(&dent, dent.d_reclen, uio);
  1058. tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
  1059. return (error);
  1060. }
  1061. /*
  1062. * Helper function for tmpfs_readdir. Creates a '..' entry for the given
  1063. * directory and returns it in the uio space. The function returns 0
  1064. * on success, -1 if there was not enough space in the uio structure to
  1065. * hold the directory entry or an appropriate error code if another
  1066. * error happens.
  1067. */
  1068. static int
  1069. tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
  1070. struct uio *uio)
  1071. {
  1072. struct tmpfs_node *parent;
  1073. struct dirent dent;
  1074. int error;
  1075. TMPFS_VALIDATE_DIR(node);
  1076. MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
  1077. /*
  1078. * Return ENOENT if the current node is already removed.
  1079. */
  1080. TMPFS_ASSERT_LOCKED(node);
  1081. parent = node->tn_dir.tn_parent;
  1082. if (parent == NULL)
  1083. return (ENOENT);
  1084. TMPFS_NODE_LOCK(parent);
  1085. dent.d_fileno = parent->tn_id;
  1086. TMPFS_NODE_UNLOCK(parent);
  1087. dent.d_type = DT_DIR;
  1088. dent.d_namlen = 2;
  1089. dent.d_name[0] = '.';
  1090. dent.d_name[1] = '.';
  1091. dent.d_reclen = GENERIC_DIRSIZ(&dent);
  1092. dirent_terminate(&dent);
  1093. if (dent.d_reclen > uio->uio_resid)
  1094. error = EJUSTRETURN;
  1095. else
  1096. error = uiomove(&dent, dent.d_reclen, uio);
  1097. tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
  1098. return (error);
  1099. }
  1100. /*
  1101. * Helper function for tmpfs_readdir. Returns as much directory entries
  1102. * as can fit in the uio space. The read starts at uio->uio_offset.
  1103. * The function returns 0 on success, -1 if there was not enough space
  1104. * in the uio structure to hold the directory entry or an appropriate
  1105. * error code if another error happens.
  1106. */
  1107. int
  1108. tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
  1109. struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
  1110. {
  1111. struct tmpfs_dir_cursor dc;
  1112. struct tmpfs_dirent *de;
  1113. off_t off;
  1114. int error;
  1115. TMPFS_VALIDATE_DIR(node);
  1116. off = 0;
  1117. /*
  1118. * Lookup the node from the current offset. The starting offset of
  1119. * 0 will lookup both '.' and '..', and then the first real entry,
  1120. * or EOF if there are none. Then find all entries for the dir that
  1121. * fit into the buffer. Once no more entries are found (de == NULL),
  1122. * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
  1123. * call to return 0.
  1124. */
  1125. switch (uio->uio_offset) {
  1126. case TMPFS_DIRCOOKIE_DOT:
  1127. error = tmpfs_dir_getdotdent(tm, node, uio);
  1128. if (error != 0)
  1129. return (error);
  1130. uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
  1131. if (cookies != NULL)
  1132. cookies[(*ncookies)++] = off = uio->uio_offset;
  1133. /* FALLTHROUGH */
  1134. case TMPFS_DIRCOOKIE_DOTDOT:
  1135. error = tmpfs_dir_getdotdotdent(tm, node, uio);
  1136. if (error != 0)
  1137. return (error);
  1138. de = tmpfs_dir_first(node, &dc);
  1139. uio->uio_offset = tmpfs_dirent_cookie(de);
  1140. if (cookies != NULL)
  1141. cookies[(*ncookies)++] = off = uio->uio_offset;
  1142. /* EOF. */
  1143. if (de == NULL)
  1144. return (0);
  1145. break;
  1146. case TMPFS_DIRCOOKIE_EOF:
  1147. return (0);
  1148. default:
  1149. de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
  1150. if (de == NULL)
  1151. return (EINVAL);
  1152. if (cookies != NULL)
  1153. off = tmpfs_dirent_cookie(de);
  1154. }
  1155. /* Read as much entries as possible; i.e., until we reach the end of
  1156. * the directory or we exhaust uio space. */
  1157. do {
  1158. struct dirent d;
  1159. /* Create a dirent structure representing the current
  1160. * tmpfs_node and fill it. */
  1161. if (de->td_node == NULL) {
  1162. d.d_fileno = 1;
  1163. d.d_type = DT_WHT;
  1164. } else {
  1165. d.d_fileno = de->td_node->tn_id;
  1166. switch (de->td_node->tn_type) {
  1167. case VBLK:
  1168. d.d_type = DT_BLK;
  1169. break;
  1170. case VCHR:
  1171. d.d_type = DT_CHR;
  1172. break;
  1173. case VDIR:
  1174. d.d_type = DT_DIR;
  1175. break;
  1176. case VFIFO:
  1177. d.d_type = DT_FIFO;
  1178. break;
  1179. case VLNK:
  1180. d.d_type = DT_LNK;
  1181. break;
  1182. case VREG:
  1183. d.d_type = DT_REG;
  1184. break;
  1185. case VSOCK:
  1186. d.d_type = DT_SOCK;
  1187. break;
  1188. default:
  1189. panic("tmpfs_dir_getdents: type %p %d",
  1190. de->td_node, (int)de->td_node->tn_type);
  1191. }
  1192. }
  1193. d.d_namlen = de->td_namelen;
  1194. MPASS(de->td_namelen < sizeof(d.d_name));
  1195. (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
  1196. d.d_reclen = GENERIC_DIRSIZ(&d);
  1197. dirent_terminate(&d);
  1198. /* Stop reading if the directory entry we are treating is
  1199. * bigger than the amount of data that can be returned. */
  1200. if (d.d_reclen > uio->uio_resid) {
  1201. error = EJUSTRETURN;
  1202. break;
  1203. }
  1204. /* Copy the new dirent structure into the output buffer and
  1205. * advance pointers. */
  1206. error = uiomove(&d, d.d_reclen, uio);
  1207. if (error == 0) {
  1208. de = tmpfs_dir_next(node, &dc);
  1209. if (cookies != NULL) {
  1210. off = tmpfs_dirent_cookie(de);
  1211. MPASS(*ncookies < maxcookies);
  1212. cookies[(*ncookies)++] = off;
  1213. }
  1214. }
  1215. } while (error == 0 && uio->uio_resid > 0 && de != NULL);
  1216. /* Skip setting off when using cookies as it is already done above. */
  1217. if (cookies == NULL)
  1218. off = tmpfs_dirent_cookie(de);
  1219. /* Update the offset and cache. */
  1220. uio->uio_offset = off;
  1221. node->tn_dir.tn_readdir_lastn = off;
  1222. node->tn_dir.tn_readdir_lastp = de;
  1223. tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
  1224. return error;
  1225. }
  1226. int
  1227. tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
  1228. {
  1229. struct tmpfs_dirent *de;
  1230. int error;
  1231. error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
  1232. cnp->cn_nameptr, cnp->cn_namelen, &de);
  1233. if (error != 0)
  1234. return (error);
  1235. tmpfs_dir_attach(dvp, de);
  1236. return (0);
  1237. }
  1238. void
  1239. tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
  1240. {
  1241. struct tmpfs_dirent *de;
  1242. de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
  1243. MPASS(de != NULL && de->td_node == NULL);
  1244. tmpfs_dir_detach(dvp, de);
  1245. tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
  1246. }
  1247. /*
  1248. * Resizes the aobj associated with the regular file pointed to by 'vp' to the
  1249. * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
  1250. * 'newsize' must be positive.
  1251. *
  1252. * Returns zero on success or an appropriate error code on failure.
  1253. */
  1254. int
  1255. tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
  1256. {
  1257. struct tmpfs_mount *tmp;
  1258. struct tmpfs_node *node;
  1259. vm_object_t uobj;
  1260. vm_page_t m;
  1261. vm_pindex_t idx, newpages, oldpages;
  1262. off_t oldsize;
  1263. int base, rv;
  1264. MPASS(vp->v_type == VREG);
  1265. MPASS(newsize >= 0);
  1266. node = VP_TO_TMPFS_NODE(vp);
  1267. uobj = node->tn_reg.tn_aobj;
  1268. tmp = VFS_TO_TMPFS(vp->v_mount);
  1269. /*
  1270. * Convert the old and new sizes to the number of pages needed to
  1271. * store them. It may happen that we do not need to do anything
  1272. * because the last allocated page can accommodate the change on
  1273. * its own.
  1274. */
  1275. oldsize = node->tn_size;
  1276. oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
  1277. MPASS(oldpages == uobj->size);
  1278. newpages = OFF_TO_IDX(newsize + PAGE_MASK);
  1279. if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
  1280. node->tn_size = newsize;
  1281. return (0);
  1282. }
  1283. if (newpages > oldpages &&
  1284. tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
  1285. return (ENOSPC);
  1286. VM_OBJECT_WLOCK(uobj);
  1287. if (newsize < oldsize) {
  1288. /*
  1289. * Zero the truncated part of the last page.
  1290. */
  1291. base = newsize & PAGE_MASK;
  1292. if (base != 0) {
  1293. idx = OFF_TO_IDX(newsize);
  1294. retry:
  1295. m = vm_page_grab(uobj, idx, VM_ALLOC_NOCREAT);
  1296. if (m != NULL) {
  1297. MPASS(vm_page_all_valid(m));
  1298. } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
  1299. m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL |
  1300. VM_ALLOC_WAITFAIL);
  1301. if (m == NULL)
  1302. goto retry;
  1303. vm_object_pip_add(uobj, 1);
  1304. VM_OBJECT_WUNLOCK(uobj);
  1305. rv = vm_pager_get_pages(uobj, &m, 1, NULL,
  1306. NULL);
  1307. VM_OBJECT_WLOCK(uobj);
  1308. vm_object_pip_wakeup(uobj);
  1309. if (rv == VM_PAGER_OK) {
  1310. /*
  1311. * Since the page was not resident,
  1312. * and therefore not recently
  1313. * accessed, immediately enqueue it
  1314. * for asynchronous laundering. The
  1315. * current operation is not regarded
  1316. * as an access.
  1317. */
  1318. vm_page_launder(m);
  1319. } else {
  1320. vm_page_free(m);
  1321. if (ignerr)
  1322. m = NULL;
  1323. else {
  1324. VM_OBJECT_WUNLOCK(uobj);
  1325. return (EIO);
  1326. }
  1327. }
  1328. }
  1329. if (m != NULL) {
  1330. pmap_zero_page_area(m, base, PAGE_SIZE - base);
  1331. vm_page_set_dirty(m);
  1332. vm_page_xunbusy(m);
  1333. }
  1334. }
  1335. /*
  1336. * Release any swap space and free any whole pages.
  1337. */
  1338. if (newpages < oldpages) {
  1339. swap_pager_freespace(uobj, newpages, oldpages -
  1340. newpages);
  1341. vm_object_page_remove(uobj, newpages, 0, 0);
  1342. }
  1343. }
  1344. uobj->size = newpages;
  1345. VM_OBJECT_WUNLOCK(uobj);
  1346. atomic_add_long(&tmp->tm_pages_used, newpages - oldpages);
  1347. node->tn_size = newsize;
  1348. return (0);
  1349. }
  1350. void
  1351. tmpfs_check_mtime(struct vnode *vp)
  1352. {
  1353. struct tmpfs_node *node;
  1354. struct vm_object *obj;
  1355. ASSERT_VOP_ELOCKED(vp, "check_mtime");
  1356. if (vp->v_type != VREG)
  1357. return;
  1358. obj = vp->v_object;
  1359. KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
  1360. (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
  1361. /* unlocked read */
  1362. if (obj->generation != obj->cleangeneration) {
  1363. VM_OBJECT_WLOCK(obj);
  1364. if (obj->generation != obj->cleangeneration) {
  1365. obj->cleangeneration = obj->generation;
  1366. node = VP_TO_TMPFS_NODE(vp);
  1367. node->tn_status |= TMPFS_NODE_MODIFIED |
  1368. TMPFS_NODE_CHANGED;
  1369. }
  1370. VM_OBJECT_WUNLOCK(obj);
  1371. }
  1372. }
  1373. /*
  1374. * Change flags of the given vnode.
  1375. * Caller should execute tmpfs_update on vp after a successful execution.
  1376. * The vnode must be locked on entry and remain locked on exit.
  1377. */
  1378. int
  1379. tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
  1380. struct thread *p)
  1381. {
  1382. int error;
  1383. struct tmpfs_node *node;
  1384. ASSERT_VOP_ELOCKED(vp, "chflags");
  1385. node = VP_TO_TMPFS_NODE(vp);
  1386. if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
  1387. UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
  1388. UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
  1389. UF_SPARSE | UF_SYSTEM)) != 0)
  1390. return (EOPNOTSUPP);
  1391. /* Disallow this operation if the file system is mounted read-only. */
  1392. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  1393. return EROFS;
  1394. /*
  1395. * Callers may only modify the file flags on objects they
  1396. * have VADMIN rights for.
  1397. */
  1398. if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
  1399. return (error);
  1400. /*
  1401. * Unprivileged processes are not permitted to unset system
  1402. * flags, or modify flags if any system flags are set.
  1403. */
  1404. if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
  1405. if (node->tn_flags &
  1406. (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
  1407. error = securelevel_gt(cred, 0);
  1408. if (error)
  1409. return (error);
  1410. }
  1411. } else {
  1412. if (node->tn_flags &
  1413. (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
  1414. ((flags ^ node->tn_flags) & SF_SETTABLE))
  1415. return (EPERM);
  1416. }
  1417. node->tn_flags = flags;
  1418. node->tn_status |= TMPFS_NODE_CHANGED;
  1419. ASSERT_VOP_ELOCKED(vp, "chflags2");
  1420. return (0);
  1421. }
  1422. /*
  1423. * Change access mode on the given vnode.
  1424. * Caller should execute tmpfs_update on vp after a successful execution.
  1425. * The vnode must be locked on entry and remain locked on exit.
  1426. */
  1427. int
  1428. tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
  1429. {
  1430. int error;
  1431. struct tmpfs_node *node;
  1432. ASSERT_VOP_ELOCKED(vp, "chmod");
  1433. node = VP_TO_TMPFS_NODE(vp);
  1434. /* Disallow this operation if the file system is mounted read-only. */
  1435. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  1436. return EROFS;
  1437. /* Immutable or append-only files cannot be modified, either. */
  1438. if (node->tn_flags & (IMMUTABLE | APPEND))
  1439. return EPERM;
  1440. /*
  1441. * To modify the permissions on a file, must possess VADMIN
  1442. * for that file.
  1443. */
  1444. if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
  1445. return (error);
  1446. /*
  1447. * Privileged processes may set the sticky bit on non-directories,
  1448. * as well as set the setgid bit on a file with a group that the
  1449. * process is not a member of.
  1450. */
  1451. if (vp->v_type != VDIR && (mode & S_ISTXT)) {
  1452. if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
  1453. return (EFTYPE);
  1454. }
  1455. if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
  1456. error = priv_check_cred(cred, PRIV_VFS_SETGID);
  1457. if (error)
  1458. return (error);
  1459. }
  1460. node->tn_mode &= ~ALLPERMS;
  1461. node->tn_mode |= mode & ALLPERMS;
  1462. node->tn_status |= TMPFS_NODE_CHANGED;
  1463. ASSERT_VOP_ELOCKED(vp, "chmod2");
  1464. return (0);
  1465. }
  1466. /*
  1467. * Change ownership of the given vnode. At least one of uid or gid must
  1468. * be different than VNOVAL. If one is set to that value, the attribute
  1469. * is unchanged.
  1470. * Caller should execute tmpfs_update on vp after a successful execution.
  1471. * The vnode must be locked on entry and remain locked on exit.
  1472. */
  1473. int
  1474. tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
  1475. struct thread *p)
  1476. {
  1477. int error;
  1478. struct tmpfs_node *node;
  1479. uid_t ouid;
  1480. gid_t ogid;
  1481. ASSERT_VOP_ELOCKED(vp, "chown");
  1482. node = VP_TO_TMPFS_NODE(vp);
  1483. /* Assign default values if they are unknown. */
  1484. MPASS(uid != VNOVAL || gid != VNOVAL);
  1485. if (uid == VNOVAL)
  1486. uid = node->tn_uid;
  1487. if (gid == VNOVAL)
  1488. gid = node->tn_gid;
  1489. MPASS(uid != VNOVAL && gid != VNOVAL);
  1490. /* Disallow this operation if the file system is mounted read-only. */
  1491. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  1492. return EROFS;
  1493. /* Immutable or append-only files cannot be modified, either. */
  1494. if (node->tn_flags & (IMMUTABLE | APPEND))
  1495. return EPERM;
  1496. /*
  1497. * To modify the ownership of a file, must possess VADMIN for that
  1498. * file.
  1499. */
  1500. if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
  1501. return (error);
  1502. /*
  1503. * To change the owner of a file, or change the group of a file to a
  1504. * group of which we are not a member, the caller must have
  1505. * privilege.
  1506. */
  1507. if ((uid != node->tn_uid ||
  1508. (gid != node->tn_gid && !groupmember(gid, cred))) &&
  1509. (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
  1510. return (error);
  1511. ogid = node->tn_gid;
  1512. ouid = node->tn_uid;
  1513. node->tn_uid = uid;
  1514. node->tn_gid = gid;
  1515. node->tn_status |= TMPFS_NODE_CHANGED;
  1516. if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
  1517. if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID))
  1518. node->tn_mode &= ~(S_ISUID | S_ISGID);
  1519. }
  1520. ASSERT_VOP_ELOCKED(vp, "chown2");
  1521. return (0);
  1522. }
  1523. /*
  1524. * Change size of the given vnode.
  1525. * Caller should execute tmpfs_update on vp after a successful execution.
  1526. * The vnode must be locked on entry and remain locked on exit.
  1527. */
  1528. int
  1529. tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
  1530. struct thread *p)
  1531. {
  1532. int error;
  1533. struct tmpfs_node *node;
  1534. ASSERT_VOP_ELOCKED(vp, "chsize");
  1535. node = VP_TO_TMPFS_NODE(vp);
  1536. /* Decide whether this is a valid operation based on the file type. */
  1537. error = 0;
  1538. switch (vp->v_type) {
  1539. case VDIR:
  1540. return EISDIR;
  1541. case VREG:
  1542. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  1543. return EROFS;
  1544. break;
  1545. case VBLK:
  1546. /* FALLTHROUGH */
  1547. case VCHR:
  1548. /* FALLTHROUGH */
  1549. case VFIFO:
  1550. /* Allow modifications of special files even if in the file
  1551. * system is mounted read-only (we are not modifying the
  1552. * files themselves, but the objects they represent). */
  1553. return 0;
  1554. default:
  1555. /* Anything else is unsupported. */
  1556. return EOPNOTSUPP;
  1557. }
  1558. /* Immutable or append-only files cannot be modified, either. */
  1559. if (node->tn_flags & (IMMUTABLE | APPEND))
  1560. return EPERM;
  1561. error = tmpfs_truncate(vp, size);
  1562. /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
  1563. * for us, as will update tn_status; no need to do that here. */
  1564. ASSERT_VOP_ELOCKED(vp, "chsize2");
  1565. return (error);
  1566. }
  1567. /*
  1568. * Change access and modification times of the given vnode.
  1569. * Caller should execute tmpfs_update on vp after a successful execution.
  1570. * The vnode must be locked on entry and remain locked on exit.
  1571. */
  1572. int
  1573. tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
  1574. struct ucred *cred, struct thread *l)
  1575. {
  1576. int error;
  1577. struct tmpfs_node *node;
  1578. ASSERT_VOP_ELOCKED(vp, "chtimes");
  1579. node = VP_TO_TMPFS_NODE(vp);
  1580. /* Disallow this operation if the file system is mounted read-only. */
  1581. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  1582. return EROFS;
  1583. /* Immutable or append-only files cannot be modified, either. */
  1584. if (node->tn_flags & (IMMUTABLE | APPEND))
  1585. return EPERM;
  1586. error = vn_utimes_perm(vp, vap, cred, l);
  1587. if (error != 0)
  1588. return (error);
  1589. if (vap->va_atime.tv_sec != VNOVAL)
  1590. node->tn_status |= TMPFS_NODE_ACCESSED;
  1591. if (vap->va_mtime.tv_sec != VNOVAL)
  1592. node->tn_status |= TMPFS_NODE_MODIFIED;
  1593. if (vap->va_birthtime.tv_sec != VNOVAL)
  1594. node->tn_status |= TMPFS_NODE_MODIFIED;
  1595. tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
  1596. if (vap->va_birthtime.tv_sec != VNOVAL)
  1597. node->tn_birthtime = vap->va_birthtime;
  1598. ASSERT_VOP_ELOCKED(vp, "chtimes2");
  1599. return (0);
  1600. }
  1601. void
  1602. tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
  1603. {
  1604. if ((node->tn_status & status) == status || tm->tm_ronly)
  1605. return;
  1606. TMPFS_NODE_LOCK(node);
  1607. node->tn_status |= status;
  1608. TMPFS_NODE_UNLOCK(node);
  1609. }
  1610. /* Sync timestamps */
  1611. void
  1612. tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
  1613. const struct timespec *mod)
  1614. {
  1615. struct tmpfs_node *node;
  1616. struct timespec now;
  1617. ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
  1618. node = VP_TO_TMPFS_NODE(vp);
  1619. if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
  1620. TMPFS_NODE_CHANGED)) == 0)
  1621. return;
  1622. vfs_timestamp(&now);
  1623. TMPFS_NODE_LOCK(node);
  1624. if (node->tn_status & TMPFS_NODE_ACCESSED) {
  1625. if (acc == NULL)
  1626. acc = &now;
  1627. node->tn_atime = *acc;
  1628. }
  1629. if (node->tn_status & TMPFS_NODE_MODIFIED) {
  1630. if (mod == NULL)
  1631. mod = &now;
  1632. node->tn_mtime = *mod;
  1633. }
  1634. if (node->tn_status & TMPFS_NODE_CHANGED)
  1635. node->tn_ctime = now;
  1636. node->tn_status &= ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
  1637. TMPFS_NODE_CHANGED);
  1638. TMPFS_NODE_UNLOCK(node);
  1639. /* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
  1640. random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
  1641. }
  1642. int
  1643. tmpfs_truncate(struct vnode *vp, off_t length)
  1644. {
  1645. int error;
  1646. struct tmpfs_node *node;
  1647. node = VP_TO_TMPFS_NODE(vp);
  1648. if (length < 0) {
  1649. error = EINVAL;
  1650. goto out;
  1651. }
  1652. if (node->tn_size == length) {
  1653. error = 0;
  1654. goto out;
  1655. }
  1656. if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
  1657. return (EFBIG);
  1658. error = tmpfs_reg_resize(vp, length, FALSE);
  1659. if (error == 0)
  1660. node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
  1661. out:
  1662. tmpfs_update(vp);
  1663. return (error);
  1664. }
  1665. static __inline int
  1666. tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
  1667. {
  1668. if (a->td_hash > b->td_hash)
  1669. return (1);
  1670. else if (a->td_hash < b->td_hash)
  1671. return (-1);
  1672. return (0);
  1673. }
  1674. RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);