tmpfs_subr.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* $OpenBSD: tmpfs_subr.c,v 1.14 2015/04/17 04:43:21 guenther Exp $ */
  2. /* $NetBSD: tmpfs_subr.c,v 1.79 2012/03/13 18:40:50 elad Exp $ */
  3. /*
  4. * Copyright (c) 2005-2011 The NetBSD Foundation, Inc.
  5. * Copyright (c) 2013 Pedro Martelletto
  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, and by Mindaugas Rasiukevicius.
  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: interfaces for inode and directory entry
  35. * construction, destruction and manipulation.
  36. *
  37. * Reference counting
  38. *
  39. * The link count of inode (tmpfs_node_t::tn_links) is used as a
  40. * reference counter. However, it has slightly different semantics.
  41. *
  42. * For directories - link count represents directory entries, which
  43. * refer to the directories. In other words, it represents the count
  44. * of sub-directories. It also takes into account the virtual '.'
  45. * entry (which has no real entry in the list). For files - link count
  46. * represents the hard links. Since only empty directories can be
  47. * removed - link count aligns the reference counting requirements
  48. * enough. Note: to check whether directory is not empty, the inode
  49. * size (tmpfs_node_t::tn_size) can be used.
  50. *
  51. * The inode itself, as an object, gathers its first reference when
  52. * directory entry is attached via tmpfs_dir_attach(9). For instance,
  53. * after regular tmpfs_create(), a file would have a link count of 1,
  54. * while directory after tmpfs_mkdir() would have 2 (due to '.').
  55. *
  56. * Reclamation
  57. *
  58. * It should be noted that tmpfs inodes rely on a combination of vnode
  59. * reference counting and link counting. That is, an inode can only be
  60. * destroyed if its associated vnode is inactive. The destruction is
  61. * done on vnode reclamation i.e. tmpfs_reclaim(). It should be noted
  62. * that tmpfs_node_t::tn_links being 0 is a destruction criterion.
  63. *
  64. * If an inode has references within the file system (tn_links > 0) and
  65. * its inactive vnode gets reclaimed/recycled - then the association is
  66. * broken in tmpfs_reclaim(). In such case, an inode will always pass
  67. * tmpfs_lookup() and thus tmpfs_vnode_get() to associate a new vnode.
  68. *
  69. * Lock order
  70. *
  71. * tmpfs_node_t::tn_nlock ->
  72. * struct vnode::v_vlock ->
  73. * struct vnode::v_interlock
  74. */
  75. #include <sys/param.h>
  76. #include <sys/dirent.h>
  77. #include <sys/event.h>
  78. #include <sys/mount.h>
  79. #include <sys/namei.h>
  80. #include <sys/time.h>
  81. #include <sys/proc.h>
  82. #include <sys/stat.h>
  83. #include <sys/systm.h>
  84. #include <sys/vnode.h>
  85. #include <uvm/uvm_aobj.h>
  86. #include <tmpfs/tmpfs.h>
  87. #include <tmpfs/tmpfs_vnops.h>
  88. /* Local functions. */
  89. void tmpfs_dir_putseq(tmpfs_node_t *, tmpfs_dirent_t *);
  90. int tmpfs_dir_getdotents(tmpfs_node_t *, struct dirent *, struct uio *);
  91. /*
  92. * tmpfs_alloc_node: allocate a new inode of a specified type and
  93. * insert it into the list of specified mount point.
  94. */
  95. int
  96. tmpfs_alloc_node(tmpfs_mount_t *tmp, enum vtype type, uid_t uid, gid_t gid,
  97. mode_t mode, char *target, dev_t rdev, tmpfs_node_t **node)
  98. {
  99. tmpfs_node_t *nnode;
  100. struct uvm_object *uobj;
  101. nnode = tmpfs_node_get(tmp);
  102. if (nnode == NULL) {
  103. return ENOSPC;
  104. }
  105. /* Initially, no references and no associations. */
  106. nnode->tn_links = 0;
  107. nnode->tn_vnode = NULL;
  108. nnode->tn_dirent_hint = NULL;
  109. rw_enter_write(&tmp->tm_acc_lock);
  110. nnode->tn_id = ++tmp->tm_highest_inode;
  111. if (nnode->tn_id == 0) {
  112. --tmp->tm_highest_inode;
  113. rw_exit_write(&tmp->tm_acc_lock);
  114. tmpfs_node_put(tmp, nnode);
  115. return ENOSPC;
  116. }
  117. rw_exit_write(&tmp->tm_acc_lock);
  118. /* Generic initialization. */
  119. nnode->tn_type = type;
  120. nnode->tn_size = 0;
  121. nnode->tn_flags = 0;
  122. nnode->tn_lockf = NULL;
  123. nnode->tn_gen = TMPFS_NODE_GEN_MASK & arc4random();
  124. nanotime(&nnode->tn_atime);
  125. nnode->tn_birthtime = nnode->tn_atime;
  126. nnode->tn_ctime = nnode->tn_atime;
  127. nnode->tn_mtime = nnode->tn_atime;
  128. /* XXX pedro: we should check for UID_MAX and GID_MAX instead. */
  129. KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
  130. nnode->tn_uid = uid;
  131. nnode->tn_gid = gid;
  132. nnode->tn_mode = mode;
  133. /* Type-specific initialization. */
  134. switch (nnode->tn_type) {
  135. case VBLK:
  136. case VCHR:
  137. /* Character/block special device. */
  138. KASSERT(rdev != VNOVAL);
  139. nnode->tn_spec.tn_dev.tn_rdev = rdev;
  140. break;
  141. case VDIR:
  142. /* Directory. */
  143. TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
  144. nnode->tn_spec.tn_dir.tn_parent = NULL;
  145. nnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
  146. nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
  147. /* Extra link count for the virtual '.' entry. */
  148. nnode->tn_links++;
  149. break;
  150. case VFIFO:
  151. case VSOCK:
  152. break;
  153. case VLNK:
  154. /* Symbolic link. Target specifies the file name. */
  155. KASSERT(target && strlen(target) < MAXPATHLEN);
  156. nnode->tn_size = strlen(target);
  157. if (nnode->tn_size == 0) {
  158. nnode->tn_spec.tn_lnk.tn_link = NULL;
  159. break;
  160. }
  161. nnode->tn_spec.tn_lnk.tn_link =
  162. tmpfs_strname_alloc(tmp, nnode->tn_size);
  163. if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
  164. tmpfs_node_put(tmp, nnode);
  165. return ENOSPC;
  166. }
  167. memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
  168. break;
  169. case VREG:
  170. /* Regular file. Create an underlying UVM object. */
  171. uobj = uao_create(0, UAO_FLAG_CANFAIL);
  172. if (uobj == NULL) {
  173. tmpfs_node_put(tmp, nnode);
  174. return ENOSPC;
  175. }
  176. nnode->tn_spec.tn_reg.tn_aobj = uobj;
  177. nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
  178. nnode->tn_spec.tn_reg.tn_aobj_pgptr = (vaddr_t)NULL;
  179. nnode->tn_spec.tn_reg.tn_aobj_pgnum = (voff_t)-1;
  180. break;
  181. default:
  182. KASSERT(0);
  183. }
  184. rw_init(&nnode->tn_nlock, "tvlk");
  185. rw_enter_write(&tmp->tm_lock);
  186. LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
  187. rw_exit_write(&tmp->tm_lock);
  188. *node = nnode;
  189. return 0;
  190. }
  191. /*
  192. * tmpfs_free_node: remove the inode from a list in the mount point and
  193. * destroy the inode structures.
  194. */
  195. void
  196. tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node)
  197. {
  198. size_t objsz;
  199. rw_enter_write(&tmp->tm_lock);
  200. LIST_REMOVE(node, tn_entries);
  201. rw_exit_write(&tmp->tm_lock);
  202. switch (node->tn_type) {
  203. case VLNK:
  204. if (node->tn_size > 0) {
  205. KASSERT(node->tn_size <= SIZE_MAX);
  206. tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
  207. node->tn_size);
  208. }
  209. break;
  210. case VREG:
  211. /*
  212. * Calculate the size of inode data, decrease the used-memory
  213. * counter, and destroy the underlying UVM object (if any).
  214. */
  215. objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
  216. if (objsz != 0) {
  217. tmpfs_mem_decr(tmp, objsz);
  218. }
  219. if (node->tn_spec.tn_reg.tn_aobj != NULL) {
  220. uao_detach(node->tn_spec.tn_reg.tn_aobj);
  221. node->tn_spec.tn_reg.tn_aobj = NULL;
  222. }
  223. break;
  224. case VDIR:
  225. KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir));
  226. KASSERT(node->tn_spec.tn_dir.tn_parent == NULL ||
  227. node == tmp->tm_root);
  228. break;
  229. default:
  230. break;
  231. }
  232. rw_enter_write(&tmp->tm_acc_lock);
  233. if (node->tn_id == tmp->tm_highest_inode)
  234. --tmp->tm_highest_inode;
  235. rw_exit_write(&tmp->tm_acc_lock);
  236. /* mutex_destroy(&node->tn_nlock); */
  237. tmpfs_node_put(tmp, node);
  238. }
  239. /*
  240. * tmpfs_vnode_get: allocate or reclaim a vnode for a specified inode.
  241. *
  242. * => Must be called with tmpfs_node_t::tn_nlock held.
  243. * => Returns vnode (*vpp) locked.
  244. */
  245. int
  246. tmpfs_vnode_get(struct mount *mp, tmpfs_node_t *node, struct vnode **vpp)
  247. {
  248. struct vnode *vp, *nvp;
  249. /* kmutex_t *slock; */
  250. int error;
  251. again:
  252. /* If there is already a vnode, try to reclaim it. */
  253. if ((vp = node->tn_vnode) != NULL) {
  254. /* atomic_or_ulong(&node->tn_gen, TMPFS_RECLAIMING_BIT); */
  255. node->tn_gen |= TMPFS_RECLAIMING_BIT;
  256. rw_exit_write(&node->tn_nlock);
  257. error = vget(vp, LK_EXCLUSIVE, curproc);
  258. if (error == ENOENT) {
  259. rw_enter_write(&node->tn_nlock);
  260. goto again;
  261. }
  262. /* atomic_and_ulong(&node->tn_gen, ~TMPFS_RECLAIMING_BIT); */
  263. node->tn_gen &= ~TMPFS_RECLAIMING_BIT;
  264. *vpp = vp;
  265. return error;
  266. }
  267. if (TMPFS_NODE_RECLAIMING(node)) {
  268. /* atomic_and_ulong(&node->tn_gen, ~TMPFS_RECLAIMING_BIT); */
  269. node->tn_gen &= ~TMPFS_RECLAIMING_BIT;
  270. }
  271. /*
  272. * Get a new vnode and associate it with our inode. Share the
  273. * lock with underlying UVM object, if there is one (VREG case).
  274. */
  275. #if 0
  276. if (node->tn_type == VREG) {
  277. struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
  278. slock = uobj->vmobjlock;
  279. } else {
  280. slock = NULL;
  281. }
  282. #endif
  283. error = getnewvnode(VT_TMPFS, mp, &tmpfs_vops, &vp);
  284. if (error) {
  285. rw_exit_write(&node->tn_nlock);
  286. return error;
  287. }
  288. lockinit(&node->tn_vlock, PINOD, "tnode", 0, 0);
  289. vp->v_type = node->tn_type;
  290. /* Type-specific initialization. */
  291. switch (node->tn_type) {
  292. case VBLK:
  293. case VCHR:
  294. vp->v_op = &tmpfs_specvops;
  295. if ((nvp = checkalias(vp, node->tn_spec.tn_dev.tn_rdev, mp))) {
  296. nvp->v_data = vp->v_data;
  297. vp->v_data = NULL;
  298. vp->v_op = &spec_vops;
  299. vrele(vp);
  300. vgone(vp);
  301. vp = nvp;
  302. node->tn_vnode = vp;
  303. }
  304. break;
  305. case VDIR:
  306. vp->v_flag |= node->tn_spec.tn_dir.tn_parent == node ?
  307. VROOT : 0;
  308. break;
  309. #ifdef FIFO
  310. case VFIFO:
  311. vp->v_op = &tmpfs_fifovops;
  312. break;
  313. #endif
  314. case VLNK:
  315. case VREG:
  316. case VSOCK:
  317. break;
  318. default:
  319. KASSERT(0);
  320. }
  321. uvm_vnp_setsize(vp, node->tn_size);
  322. vp->v_data = node;
  323. node->tn_vnode = vp;
  324. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
  325. rw_exit_write(&node->tn_nlock);
  326. KASSERT(VOP_ISLOCKED(vp));
  327. *vpp = vp;
  328. return 0;
  329. }
  330. /*
  331. * tmpfs_alloc_file: allocate a new file of specified type and adds it
  332. * into the parent directory.
  333. *
  334. * => Credentials of the caller are used.
  335. */
  336. int
  337. tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
  338. struct componentname *cnp, char *target)
  339. {
  340. tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
  341. tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
  342. tmpfs_dirent_t *de;
  343. int error;
  344. KASSERT(VOP_ISLOCKED(dvp));
  345. *vpp = NULL;
  346. /* Check for the maximum number of links limit. */
  347. if (vap->va_type == VDIR) {
  348. /* Check for maximum links limit. */
  349. if (dnode->tn_links == LINK_MAX) {
  350. error = EMLINK;
  351. goto out;
  352. }
  353. KASSERT(dnode->tn_links < LINK_MAX);
  354. }
  355. if (TMPFS_DIRSEQ_FULL(dnode)) {
  356. error = ENOSPC;
  357. goto out;
  358. }
  359. if (dnode->tn_links == 0) {
  360. error = ENOENT;
  361. goto out;
  362. }
  363. /* Allocate a node that represents the new file. */
  364. error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
  365. dnode->tn_gid, vap->va_mode, target, vap->va_rdev, &node);
  366. if (error)
  367. goto out;
  368. /* Allocate a directory entry that points to the new file. */
  369. error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
  370. if (error) {
  371. tmpfs_free_node(tmp, node);
  372. goto out;
  373. }
  374. /* Get a vnode for the new file. */
  375. rw_enter_write(&node->tn_nlock);
  376. error = tmpfs_vnode_get(dvp->v_mount, node, vpp);
  377. if (error) {
  378. tmpfs_free_dirent(tmp, de);
  379. tmpfs_free_node(tmp, node);
  380. goto out;
  381. }
  382. /* Associate inode and attach the entry into the directory. */
  383. tmpfs_dir_attach(dnode, de, node);
  384. out:
  385. if (error == 0 && (cnp->cn_flags & SAVESTART) == 0)
  386. pool_put(&namei_pool, cnp->cn_pnbuf);
  387. vput(dvp);
  388. return error;
  389. }
  390. /*
  391. * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
  392. * The directory entry contains a path name component.
  393. */
  394. int
  395. tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
  396. tmpfs_dirent_t **de)
  397. {
  398. tmpfs_dirent_t *nde;
  399. nde = tmpfs_dirent_get(tmp);
  400. if (nde == NULL)
  401. return ENOSPC;
  402. nde->td_name = tmpfs_strname_alloc(tmp, len);
  403. if (nde->td_name == NULL) {
  404. tmpfs_dirent_put(tmp, nde);
  405. return ENOSPC;
  406. }
  407. nde->td_namelen = len;
  408. memcpy(nde->td_name, name, len);
  409. nde->td_seq = TMPFS_DIRSEQ_NONE;
  410. *de = nde;
  411. return 0;
  412. }
  413. /*
  414. * tmpfs_free_dirent: free a directory entry.
  415. */
  416. void
  417. tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
  418. {
  419. KASSERT(de->td_node == NULL);
  420. KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
  421. tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
  422. tmpfs_dirent_put(tmp, de);
  423. }
  424. /*
  425. * tmpfs_dir_attach: associate directory entry with a specified inode,
  426. * and attach the entry into the directory, specified by vnode.
  427. *
  428. * => Increases link count on the associated node.
  429. * => Increases link count on directory node, if our node is VDIR.
  430. * It is caller's responsibility to check for the LINK_MAX limit.
  431. * => Triggers kqueue events here.
  432. */
  433. void
  434. tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node)
  435. {
  436. struct vnode *dvp = dnode->tn_vnode;
  437. int events = NOTE_WRITE;
  438. KASSERT(dvp != NULL);
  439. KASSERT(VOP_ISLOCKED(dvp));
  440. /* Get a new sequence number. */
  441. KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
  442. de->td_seq = tmpfs_dir_getseq(dnode, de);
  443. /* Associate directory entry and the inode. */
  444. de->td_node = node;
  445. KASSERT(node->tn_links < LINK_MAX);
  446. node->tn_links++;
  447. /* Save the hint (might overwrite). */
  448. node->tn_dirent_hint = de;
  449. /* Insert the entry to the directory (parent of inode). */
  450. TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
  451. dnode->tn_size += sizeof(tmpfs_dirent_t);
  452. tmpfs_update(dnode, TMPFS_NODE_STATUSALL);
  453. uvm_vnp_setsize(dvp, dnode->tn_size);
  454. if (node->tn_type == VDIR) {
  455. /* Set parent. */
  456. KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
  457. node->tn_spec.tn_dir.tn_parent = dnode;
  458. /* Increase the link count of parent. */
  459. KASSERT(dnode->tn_links < LINK_MAX);
  460. dnode->tn_links++;
  461. events |= NOTE_LINK;
  462. TMPFS_VALIDATE_DIR(node);
  463. }
  464. VN_KNOTE(dvp, events);
  465. }
  466. /*
  467. * tmpfs_dir_detach: disassociate directory entry and its inode,
  468. * and detach the entry from the directory, specified by vnode.
  469. *
  470. * => Decreases link count on the associated node.
  471. * => Decreases the link count on directory node, if our node is VDIR.
  472. * => Triggers kqueue events here.
  473. */
  474. void
  475. tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
  476. {
  477. tmpfs_node_t *node = de->td_node;
  478. struct vnode *vp, *dvp = dnode->tn_vnode;
  479. int events = NOTE_WRITE;
  480. KASSERT(dvp == NULL || VOP_ISLOCKED(dvp));
  481. /* Deassociate the inode and entry. */
  482. de->td_node = NULL;
  483. node->tn_dirent_hint = NULL;
  484. KASSERT(node->tn_links > 0);
  485. node->tn_links--;
  486. if ((vp = node->tn_vnode) != NULL) {
  487. KASSERT(VOP_ISLOCKED(vp));
  488. VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
  489. }
  490. /* If directory - decrease the link count of parent. */
  491. if (node->tn_type == VDIR) {
  492. KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
  493. node->tn_spec.tn_dir.tn_parent = NULL;
  494. KASSERT(dnode->tn_links > 0);
  495. dnode->tn_links--;
  496. events |= NOTE_LINK;
  497. }
  498. /* Remove the entry from the directory. */
  499. if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
  500. dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
  501. }
  502. TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
  503. dnode->tn_size -= sizeof(tmpfs_dirent_t);
  504. tmpfs_update(dnode, TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
  505. tmpfs_dir_putseq(dnode, de);
  506. if (dvp) {
  507. tmpfs_update(dnode, 0);
  508. uvm_vnp_setsize(dvp, dnode->tn_size);
  509. VN_KNOTE(dvp, events);
  510. }
  511. }
  512. /*
  513. * tmpfs_dir_lookup: find a directory entry in the specified inode.
  514. *
  515. * Note that the . and .. components are not allowed as they do not
  516. * physically exist within directories.
  517. */
  518. tmpfs_dirent_t *
  519. tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
  520. {
  521. const char *name = cnp->cn_nameptr;
  522. const uint16_t nlen = cnp->cn_namelen;
  523. tmpfs_dirent_t *de;
  524. KASSERT(VOP_ISLOCKED(node->tn_vnode));
  525. KASSERT(nlen != 1 || !(name[0] == '.'));
  526. KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
  527. TMPFS_VALIDATE_DIR(node);
  528. TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
  529. if (de->td_namelen != nlen)
  530. continue;
  531. if (memcmp(de->td_name, name, nlen) != 0)
  532. continue;
  533. break;
  534. }
  535. tmpfs_update(node, TMPFS_NODE_ACCESSED);
  536. return de;
  537. }
  538. /*
  539. * tmpfs_dir_cached: get a cached directory entry if it is valid. Used to
  540. * avoid unnecessary tmpfs_dir_lookup().
  541. *
  542. * => The vnode must be locked.
  543. */
  544. tmpfs_dirent_t *
  545. tmpfs_dir_cached(tmpfs_node_t *node)
  546. {
  547. tmpfs_dirent_t *de = node->tn_dirent_hint;
  548. KASSERT(VOP_ISLOCKED(node->tn_vnode));
  549. if (de == NULL) {
  550. return NULL;
  551. }
  552. KASSERT(de->td_node == node);
  553. /*
  554. * Directories always have a valid hint. For files, check if there
  555. * are any hard links. If there are - hint might be invalid.
  556. */
  557. return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
  558. }
  559. /*
  560. * tmpfs_dir_getseq: get a per-directory sequence number for the entry.
  561. */
  562. uint64_t
  563. tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
  564. {
  565. uint64_t seq = de->td_seq;
  566. TMPFS_VALIDATE_DIR(dnode);
  567. if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) {
  568. /* Already set. */
  569. KASSERT(seq >= TMPFS_DIRSEQ_START);
  570. return seq;
  571. }
  572. /*
  573. * The "." and ".." and the end-of-directory have reserved numbers.
  574. * The other sequence numbers are allocated incrementally.
  575. */
  576. seq = dnode->tn_spec.tn_dir.tn_next_seq;
  577. KASSERT(seq >= TMPFS_DIRSEQ_START);
  578. KASSERT(seq < TMPFS_DIRSEQ_END);
  579. dnode->tn_spec.tn_dir.tn_next_seq++;
  580. return seq;
  581. }
  582. void
  583. tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
  584. {
  585. uint64_t seq = de->td_seq;
  586. TMPFS_VALIDATE_DIR(dnode);
  587. KASSERT(seq == TMPFS_DIRSEQ_NONE || seq >= TMPFS_DIRSEQ_START);
  588. KASSERT(seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END);
  589. de->td_seq = TMPFS_DIRSEQ_NONE;
  590. /* Empty? We can reset. */
  591. if (dnode->tn_size == 0) {
  592. dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
  593. } else if (seq != TMPFS_DIRSEQ_NONE &&
  594. seq == dnode->tn_spec.tn_dir.tn_next_seq - 1) {
  595. dnode->tn_spec.tn_dir.tn_next_seq--;
  596. }
  597. }
  598. /*
  599. * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number.
  600. */
  601. tmpfs_dirent_t *
  602. tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq)
  603. {
  604. tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp;
  605. TMPFS_VALIDATE_DIR(node);
  606. /*
  607. * First, check the cache. If does not match - perform a lookup.
  608. */
  609. if (de && de->td_seq == seq) {
  610. KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
  611. KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
  612. return de;
  613. }
  614. TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
  615. KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
  616. KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
  617. if (de->td_seq == seq)
  618. return de;
  619. }
  620. return NULL;
  621. }
  622. /*
  623. * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the
  624. * dot meta entries, that is, "." or "..". Copy it to the UIO space.
  625. */
  626. int
  627. tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio)
  628. {
  629. tmpfs_dirent_t *de;
  630. off_t next = 0;
  631. int error;
  632. switch (uio->uio_offset) {
  633. case TMPFS_DIRSEQ_DOT:
  634. dp->d_fileno = node->tn_id;
  635. strlcpy(dp->d_name, ".", sizeof(dp->d_name));
  636. next = TMPFS_DIRSEQ_DOTDOT;
  637. break;
  638. case TMPFS_DIRSEQ_DOTDOT:
  639. dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
  640. strlcpy(dp->d_name, "..", sizeof(dp->d_name));
  641. de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
  642. next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
  643. break;
  644. default:
  645. KASSERT(false);
  646. }
  647. dp->d_type = DT_DIR;
  648. dp->d_namlen = strlen(dp->d_name);
  649. dp->d_reclen = DIRENT_SIZE(dp);
  650. dp->d_off = next;
  651. if (dp->d_reclen > uio->uio_resid) {
  652. return EJUSTRETURN;
  653. }
  654. if ((error = uiomovei(dp, dp->d_reclen, uio)) != 0) {
  655. return error;
  656. }
  657. uio->uio_offset = next;
  658. return error;
  659. }
  660. /*
  661. * tmpfs_dir_getdents: helper function for tmpfs_readdir.
  662. *
  663. * => Returns as much directory entries as can fit in the uio space.
  664. * => The read starts at uio->uio_offset.
  665. */
  666. int
  667. tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio)
  668. {
  669. tmpfs_dirent_t *de, *next_de;
  670. struct dirent dent;
  671. int error = 0;
  672. KASSERT(VOP_ISLOCKED(node->tn_vnode));
  673. TMPFS_VALIDATE_DIR(node);
  674. memset(&dent, 0, sizeof(dent));
  675. if (uio->uio_offset == TMPFS_DIRSEQ_DOT) {
  676. if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
  677. goto done;
  678. }
  679. }
  680. if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) {
  681. if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
  682. goto done;
  683. }
  684. }
  685. /* Done if we reached the end. */
  686. if (uio->uio_offset == TMPFS_DIRSEQ_EOF) {
  687. goto done;
  688. }
  689. /* Locate the directory entry given by the given sequence number. */
  690. de = tmpfs_dir_lookupbyseq(node, uio->uio_offset);
  691. if (de == NULL) {
  692. error = EINVAL;
  693. goto done;
  694. }
  695. /*
  696. * Read as many entries as possible; i.e., until we reach the end
  697. * of the directory or we exhaust UIO space.
  698. */
  699. do {
  700. dent.d_fileno = de->td_node->tn_id;
  701. switch (de->td_node->tn_type) {
  702. case VBLK:
  703. dent.d_type = DT_BLK;
  704. break;
  705. case VCHR:
  706. dent.d_type = DT_CHR;
  707. break;
  708. case VDIR:
  709. dent.d_type = DT_DIR;
  710. break;
  711. case VFIFO:
  712. dent.d_type = DT_FIFO;
  713. break;
  714. case VLNK:
  715. dent.d_type = DT_LNK;
  716. break;
  717. case VREG:
  718. dent.d_type = DT_REG;
  719. break;
  720. case VSOCK:
  721. dent.d_type = DT_SOCK;
  722. break;
  723. default:
  724. KASSERT(0);
  725. }
  726. dent.d_namlen = de->td_namelen;
  727. KASSERT(de->td_namelen < sizeof(dent.d_name));
  728. memcpy(dent.d_name, de->td_name, de->td_namelen);
  729. dent.d_name[de->td_namelen] = '\0';
  730. dent.d_reclen = DIRENT_SIZE(&dent);
  731. next_de = TAILQ_NEXT(de, td_entries);
  732. if (next_de == NULL)
  733. dent.d_off = TMPFS_DIRSEQ_EOF;
  734. else
  735. dent.d_off = tmpfs_dir_getseq(node, next_de);
  736. if (dent.d_reclen > uio->uio_resid) {
  737. /* Exhausted UIO space. */
  738. error = EJUSTRETURN;
  739. break;
  740. }
  741. /* Copy out the directory entry and continue. */
  742. error = uiomovei(&dent, dent.d_reclen, uio);
  743. if (error) {
  744. break;
  745. }
  746. de = TAILQ_NEXT(de, td_entries);
  747. } while (uio->uio_resid > 0 && de);
  748. /* Cache the last entry or clear and mark EOF. */
  749. uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
  750. node->tn_spec.tn_dir.tn_readdir_lastp = de;
  751. done:
  752. tmpfs_update(node, TMPFS_NODE_ACCESSED);
  753. if (error == EJUSTRETURN) {
  754. /* Exhausted UIO space - just return. */
  755. error = 0;
  756. }
  757. KASSERT(error >= 0);
  758. return error;
  759. }
  760. /*
  761. * tmpfs_reg_resize: resize the underlying UVM object associated with the
  762. * specified regular file.
  763. */
  764. int
  765. tmpfs_reg_resize(struct vnode *vp, off_t newsize)
  766. {
  767. tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
  768. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  769. struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
  770. size_t newpages, oldpages, bytes;
  771. off_t oldsize;
  772. vaddr_t pgoff;
  773. int error;
  774. KASSERT(vp->v_type == VREG);
  775. KASSERT(newsize >= 0);
  776. oldsize = node->tn_size;
  777. oldpages = round_page(oldsize) >> PAGE_SHIFT;
  778. newpages = round_page(newsize) >> PAGE_SHIFT;
  779. KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
  780. if (newpages > oldpages) {
  781. /* Increase the used-memory counter if getting extra pages. */
  782. bytes = (newpages - oldpages) << PAGE_SHIFT;
  783. if (tmpfs_mem_incr(tmp, bytes) == 0)
  784. return ENOSPC;
  785. if (uao_grow(uobj, newpages) != 0) {
  786. tmpfs_mem_decr(tmp, bytes);
  787. return ENOSPC;
  788. }
  789. }
  790. node->tn_spec.tn_reg.tn_aobj_pages = newpages;
  791. node->tn_size = newsize;
  792. uvm_vnp_setsize(vp, newsize);
  793. uvm_vnp_uncache(vp);
  794. /*
  795. * Free "backing store".
  796. */
  797. if (newpages < oldpages) {
  798. if (tmpfs_uio_cached(node))
  799. tmpfs_uio_uncache(node);
  800. if (uao_shrink(uobj, newpages))
  801. panic("shrink failed");
  802. /* Decrease the used-memory counter. */
  803. tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
  804. }
  805. if (newsize > oldsize) {
  806. if (tmpfs_uio_cached(node))
  807. tmpfs_uio_uncache(node);
  808. pgoff = oldsize & PAGE_MASK;
  809. if (pgoff != 0) {
  810. /*
  811. * Growing from an offset which is not at a page
  812. * boundary; zero out unused bytes in current page.
  813. */
  814. error = tmpfs_zeropg(node, trunc_page(oldsize), pgoff);
  815. if (error)
  816. panic("tmpfs_zeropg: error %d", error);
  817. }
  818. VN_KNOTE(vp, NOTE_EXTEND);
  819. }
  820. return 0;
  821. }
  822. /*
  823. * tmpfs_chflags: change flags of the given vnode.
  824. *
  825. */
  826. int
  827. tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
  828. {
  829. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  830. int error;
  831. KASSERT(VOP_ISLOCKED(vp));
  832. /* Disallow this operation if the file system is mounted read-only. */
  833. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  834. return EROFS;
  835. if (cred->cr_uid != node->tn_uid && (error = suser_ucred(cred)))
  836. return error;
  837. if (cred->cr_uid == 0) {
  838. if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND) &&
  839. securelevel > 0)
  840. return EPERM;
  841. node->tn_flags = flags;
  842. } else {
  843. if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND) ||
  844. (flags & UF_SETTABLE) != flags)
  845. return EPERM;
  846. node->tn_flags &= SF_SETTABLE;
  847. node->tn_flags |= (flags & UF_SETTABLE);
  848. }
  849. tmpfs_update(node, TMPFS_NODE_CHANGED);
  850. VN_KNOTE(vp, NOTE_ATTRIB);
  851. return 0;
  852. }
  853. /*
  854. * tmpfs_chmod: change access mode on the given vnode.
  855. *
  856. */
  857. int
  858. tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
  859. {
  860. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  861. int error;
  862. KASSERT(VOP_ISLOCKED(vp));
  863. /* Disallow this operation if the file system is mounted read-only. */
  864. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  865. return EROFS;
  866. /* Immutable or append-only files cannot be modified, either. */
  867. if (node->tn_flags & (IMMUTABLE | APPEND))
  868. return EPERM;
  869. if (cred->cr_uid != node->tn_uid && (error = suser_ucred(cred)))
  870. return error;
  871. if (cred->cr_uid != 0) {
  872. if (vp->v_type != VDIR && (mode & S_ISTXT))
  873. return EFTYPE;
  874. if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID))
  875. return EPERM;
  876. }
  877. node->tn_mode = (mode & ALLPERMS);
  878. tmpfs_update(node, TMPFS_NODE_CHANGED);
  879. if ((vp->v_flag & VTEXT) && (node->tn_mode & S_ISTXT) == 0)
  880. uvm_vnp_uncache(vp);
  881. VN_KNOTE(vp, NOTE_ATTRIB);
  882. return 0;
  883. }
  884. /*
  885. * tmpfs_chown: change ownership of the given vnode.
  886. *
  887. * => At least one of uid or gid must be different than VNOVAL.
  888. * => Attribute is unchanged for VNOVAL case.
  889. */
  890. int
  891. tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, struct proc *p)
  892. {
  893. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  894. int error;
  895. KASSERT(VOP_ISLOCKED(vp));
  896. /* Assign default values if they are unknown. */
  897. KASSERT(uid != VNOVAL || gid != VNOVAL);
  898. if (uid == VNOVAL) {
  899. uid = node->tn_uid;
  900. }
  901. if (gid == VNOVAL) {
  902. gid = node->tn_gid;
  903. }
  904. /* Disallow this operation if the file system is mounted read-only. */
  905. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  906. return EROFS;
  907. /* Immutable or append-only files cannot be modified, either. */
  908. if (node->tn_flags & (IMMUTABLE | APPEND))
  909. return EPERM;
  910. if ((cred->cr_uid != node->tn_uid || uid != node->tn_uid ||
  911. (gid != node->tn_gid && !groupmember(gid, cred))) &&
  912. (error = suser_ucred(cred)))
  913. return error;
  914. node->tn_uid = uid;
  915. node->tn_gid = gid;
  916. tmpfs_update(node, TMPFS_NODE_CHANGED);
  917. VN_KNOTE(vp, NOTE_ATTRIB);
  918. return 0;
  919. }
  920. /*
  921. * tmpfs_chsize: change size of the given vnode.
  922. */
  923. int
  924. tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, struct proc *p)
  925. {
  926. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  927. KASSERT(VOP_ISLOCKED(vp));
  928. /* Decide whether this is a valid operation based on the file type. */
  929. switch (vp->v_type) {
  930. case VDIR:
  931. return EISDIR;
  932. case VREG:
  933. if (vp->v_mount->mnt_flag & MNT_RDONLY) {
  934. return EROFS;
  935. }
  936. break;
  937. case VBLK:
  938. case VCHR:
  939. case VFIFO:
  940. /*
  941. * Allow modifications of special files even if in the file
  942. * system is mounted read-only (we are not modifying the
  943. * files themselves, but the objects they represent).
  944. */
  945. return 0;
  946. default:
  947. return EOPNOTSUPP;
  948. }
  949. /* Immutable or append-only files cannot be modified, either. */
  950. if (node->tn_flags & (IMMUTABLE | APPEND)) {
  951. return EPERM;
  952. }
  953. /* Note: tmpfs_truncate() will raise NOTE_EXTEND and NOTE_ATTRIB. */
  954. return tmpfs_truncate(vp, size);
  955. }
  956. /*
  957. * tmpfs_chtimes: change access and modification times for vnode.
  958. */
  959. int
  960. tmpfs_chtimes(struct vnode *vp, const struct timespec *atime,
  961. const struct timespec *mtime, int vaflags, struct ucred *cred,
  962. struct proc *p)
  963. {
  964. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  965. int error;
  966. KASSERT(VOP_ISLOCKED(vp));
  967. /* Disallow this operation if the file system is mounted read-only. */
  968. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  969. return EROFS;
  970. /* Immutable or append-only files cannot be modified, either. */
  971. if (node->tn_flags & (IMMUTABLE | APPEND))
  972. return EPERM;
  973. if (cred->cr_uid != node->tn_uid && (error = suser_ucred(cred)) &&
  974. ((vaflags & VA_UTIMES_NULL) == 0 ||
  975. (error = VOP_ACCESS(vp, VWRITE, cred, p))))
  976. return error;
  977. if (atime->tv_nsec != VNOVAL)
  978. node->tn_atime = *atime;
  979. if (mtime->tv_nsec != VNOVAL)
  980. node->tn_mtime = *mtime;
  981. if (mtime->tv_nsec != VNOVAL || (vaflags & VA_UTIMES_CHANGE))
  982. tmpfs_update(VP_TO_TMPFS_NODE(vp), TMPFS_NODE_CHANGED);
  983. VN_KNOTE(vp, NOTE_ATTRIB);
  984. return 0;
  985. }
  986. /*
  987. * tmpfs_update: update timestamps, et al.
  988. */
  989. void
  990. tmpfs_update(tmpfs_node_t *node, int flags)
  991. {
  992. struct timespec nowtm;
  993. nanotime(&nowtm);
  994. if (flags & TMPFS_NODE_ACCESSED) {
  995. node->tn_atime = nowtm;
  996. }
  997. if (flags & TMPFS_NODE_MODIFIED) {
  998. node->tn_mtime = nowtm;
  999. }
  1000. if (flags & TMPFS_NODE_CHANGED) {
  1001. node->tn_ctime = nowtm;
  1002. }
  1003. }
  1004. int
  1005. tmpfs_truncate(struct vnode *vp, off_t length)
  1006. {
  1007. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  1008. int error;
  1009. if (length < 0) {
  1010. error = EINVAL;
  1011. goto out;
  1012. }
  1013. if (node->tn_size == length) {
  1014. error = 0;
  1015. goto out;
  1016. }
  1017. error = tmpfs_reg_resize(vp, length);
  1018. if (error == 0) {
  1019. tmpfs_update(node, TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED);
  1020. }
  1021. out:
  1022. return error;
  1023. }
  1024. int
  1025. tmpfs_uio_cached(tmpfs_node_t *node)
  1026. {
  1027. int pgnum_valid = (node->tn_pgnum != (voff_t)-1);
  1028. int pgptr_valid = (node->tn_pgptr != (vaddr_t)NULL);
  1029. KASSERT(pgnum_valid == pgptr_valid);
  1030. return pgnum_valid && pgptr_valid;
  1031. }
  1032. vaddr_t
  1033. tmpfs_uio_lookup(tmpfs_node_t *node, voff_t pgnum)
  1034. {
  1035. if (tmpfs_uio_cached(node) == 1 && node->tn_pgnum == pgnum)
  1036. return node->tn_pgptr;
  1037. return (vaddr_t)NULL;
  1038. }
  1039. void
  1040. tmpfs_uio_uncache(tmpfs_node_t *node)
  1041. {
  1042. KASSERT(node->tn_pgnum != (voff_t)-1);
  1043. KASSERT(node->tn_pgptr != (vaddr_t)NULL);
  1044. uvm_unmap(kernel_map, node->tn_pgptr, node->tn_pgptr + PAGE_SIZE);
  1045. node->tn_pgnum = (voff_t)-1;
  1046. node->tn_pgptr = (vaddr_t)NULL;
  1047. }
  1048. void
  1049. tmpfs_uio_cache(tmpfs_node_t *node, voff_t pgnum, vaddr_t pgptr)
  1050. {
  1051. KASSERT(node->tn_pgnum == (voff_t)-1);
  1052. KASSERT(node->tn_pgptr == (vaddr_t)NULL);
  1053. node->tn_pgnum = pgnum;
  1054. node->tn_pgptr = pgptr;
  1055. }
  1056. /*
  1057. * Be gentle to kernel_map, don't allow more than 4MB in a single transaction.
  1058. */
  1059. #define TMPFS_UIO_MAXBYTES ((1 << 22) - PAGE_SIZE)
  1060. int
  1061. tmpfs_uiomove(tmpfs_node_t *node, struct uio *uio, vsize_t len)
  1062. {
  1063. vaddr_t va, pgoff;
  1064. int error, adv;
  1065. voff_t pgnum;
  1066. vsize_t sz;
  1067. pgnum = trunc_page(uio->uio_offset);
  1068. pgoff = uio->uio_offset & PAGE_MASK;
  1069. if (pgoff + len < PAGE_SIZE) {
  1070. va = tmpfs_uio_lookup(node, pgnum);
  1071. if (va != (vaddr_t)NULL)
  1072. return uiomovei((void *)va + pgoff, len, uio);
  1073. }
  1074. if (len >= TMPFS_UIO_MAXBYTES) {
  1075. sz = TMPFS_UIO_MAXBYTES;
  1076. adv = MADV_NORMAL;
  1077. } else {
  1078. sz = len;
  1079. adv = MADV_SEQUENTIAL;
  1080. }
  1081. if (tmpfs_uio_cached(node))
  1082. tmpfs_uio_uncache(node);
  1083. uao_reference(node->tn_uobj);
  1084. error = uvm_map(kernel_map, &va, round_page(pgoff + sz), node->tn_uobj,
  1085. trunc_page(uio->uio_offset), 0, UVM_MAPFLAG(PROT_READ | PROT_WRITE,
  1086. PROT_READ | PROT_WRITE, MAP_INHERIT_NONE, adv, 0));
  1087. if (error) {
  1088. uao_detach(node->tn_uobj); /* Drop reference. */
  1089. return error;
  1090. }
  1091. error = uiomovei((void *)va + pgoff, sz, uio);
  1092. if (error == 0 && pgoff + sz < PAGE_SIZE)
  1093. tmpfs_uio_cache(node, pgnum, va);
  1094. else
  1095. uvm_unmap(kernel_map, va, va + round_page(pgoff + sz));
  1096. return error;
  1097. }
  1098. int
  1099. tmpfs_zeropg(tmpfs_node_t *node, voff_t pgnum, vaddr_t pgoff)
  1100. {
  1101. vaddr_t va;
  1102. int error;
  1103. KASSERT(tmpfs_uio_cached(node) == 0);
  1104. uao_reference(node->tn_uobj);
  1105. error = uvm_map(kernel_map, &va, PAGE_SIZE, node->tn_uobj, pgnum, 0,
  1106. UVM_MAPFLAG(PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
  1107. MAP_INHERIT_NONE, MADV_NORMAL, 0));
  1108. if (error) {
  1109. uao_detach(node->tn_uobj); /* Drop reference. */
  1110. return error;
  1111. }
  1112. bzero((void *)va + pgoff, PAGE_SIZE - pgoff);
  1113. uvm_unmap(kernel_map, va, va + PAGE_SIZE);
  1114. return 0;
  1115. }