tmpfs_vnops.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /* $OpenBSD: tmpfs_vnops.c,v 1.22 2015/04/17 04:43:21 guenther Exp $ */
  2. /* $NetBSD: tmpfs_vnops.c,v 1.100 2012/11/05 17:27:39 dholland Exp $ */
  3. /*
  4. * Copyright (c) 2005, 2006, 2007, 2012 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 Taylor R Campbell.
  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. * tmpfs vnode interface.
  35. */
  36. #include <sys/param.h>
  37. #include <sys/fcntl.h>
  38. #include <sys/event.h>
  39. #include <sys/namei.h>
  40. #include <sys/stat.h>
  41. #include <sys/uio.h>
  42. #include <sys/unistd.h>
  43. #include <sys/vnode.h>
  44. #include <sys/lockf.h>
  45. #include <sys/poll.h>
  46. #include <sys/file.h>
  47. #include <miscfs/fifofs/fifo.h>
  48. #include <tmpfs/tmpfs_vnops.h>
  49. #include <tmpfs/tmpfs.h>
  50. int tmpfs_kqfilter(void *v);
  51. /*
  52. * vnode operations vector used for files stored in a tmpfs file system.
  53. */
  54. struct vops tmpfs_vops = {
  55. .vop_lookup = tmpfs_lookup,
  56. .vop_create = tmpfs_create,
  57. .vop_mknod = tmpfs_mknod,
  58. .vop_open = tmpfs_open,
  59. .vop_close = tmpfs_close,
  60. .vop_access = tmpfs_access,
  61. .vop_getattr = tmpfs_getattr,
  62. .vop_setattr = tmpfs_setattr,
  63. .vop_read = tmpfs_read,
  64. .vop_write = tmpfs_write,
  65. .vop_ioctl = tmpfs_ioctl,
  66. .vop_poll = tmpfs_poll,
  67. .vop_kqfilter = tmpfs_kqfilter,
  68. .vop_revoke = vop_generic_revoke,
  69. .vop_fsync = tmpfs_fsync,
  70. .vop_remove = tmpfs_remove,
  71. .vop_link = tmpfs_link,
  72. .vop_rename = tmpfs_rename,
  73. .vop_mkdir = tmpfs_mkdir,
  74. .vop_rmdir = tmpfs_rmdir,
  75. .vop_symlink = tmpfs_symlink,
  76. .vop_readdir = tmpfs_readdir,
  77. .vop_readlink = tmpfs_readlink,
  78. .vop_abortop = vop_generic_abortop,
  79. .vop_inactive = tmpfs_inactive,
  80. .vop_reclaim = tmpfs_reclaim,
  81. .vop_lock = tmpfs_lock,
  82. .vop_unlock = tmpfs_unlock,
  83. .vop_bmap = vop_generic_bmap,
  84. .vop_strategy = tmpfs_strategy,
  85. .vop_print = tmpfs_print,
  86. .vop_islocked = tmpfs_islocked,
  87. .vop_pathconf = tmpfs_pathconf,
  88. .vop_advlock = tmpfs_advlock,
  89. .vop_bwrite = tmpfs_bwrite,
  90. };
  91. /*
  92. * tmpfs_lookup: path name traversal routine.
  93. *
  94. * Arguments: dvp (directory being searched), vpp (result),
  95. * cnp (component name - path).
  96. *
  97. * => Caller holds a reference and lock on dvp.
  98. * => We return looked-up vnode (vpp) locked, with a reference held.
  99. */
  100. int
  101. tmpfs_lookup(void *v)
  102. {
  103. struct vop_lookup_args /* {
  104. struct vnode *a_dvp;
  105. struct vnode **a_vpp;
  106. struct componentname *a_cnp;
  107. } */ *ap = v;
  108. struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
  109. struct componentname *cnp = ap->a_cnp;
  110. struct ucred *cred = cnp->cn_cred;
  111. const int lastcn = (cnp->cn_flags & ISLASTCN) != 0;
  112. const int lockparent = (cnp->cn_flags & LOCKPARENT) != 0;
  113. tmpfs_node_t *dnode, *tnode;
  114. tmpfs_dirent_t *de;
  115. int cachefound;
  116. int error;
  117. KASSERT(VOP_ISLOCKED(dvp));
  118. dnode = VP_TO_TMPFS_DIR(dvp);
  119. cnp->cn_flags &= ~PDIRUNLOCK;
  120. *vpp = NULL;
  121. /* Check accessibility of directory. */
  122. error = VOP_ACCESS(dvp, VEXEC, cred, curproc);
  123. if (error) {
  124. goto out;
  125. }
  126. /*
  127. * If requesting the last path component on a read-only file system
  128. * with a write operation, deny it.
  129. */
  130. if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
  131. (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  132. error = EROFS;
  133. goto out;
  134. }
  135. /*
  136. * Avoid doing a linear scan of the directory if the requested
  137. * directory/name couple is already in the cache.
  138. */
  139. cachefound = cache_lookup(dvp, vpp, cnp);
  140. if (cachefound == ENOENT /* && *vpp == NULLVP */)
  141. return ENOENT; /* Negative cache hit. */
  142. else if (cachefound != -1)
  143. return 0; /* Found in cache. */
  144. if (cnp->cn_flags & ISDOTDOT) {
  145. tmpfs_node_t *pnode;
  146. /*
  147. * Lookup of ".." case.
  148. */
  149. if (lastcn) {
  150. if (cnp->cn_nameiop == RENAME) {
  151. error = EINVAL;
  152. goto out;
  153. }
  154. if (cnp->cn_nameiop == DELETE) {
  155. /* Keep the name for tmpfs_rmdir(). */
  156. cnp->cn_flags |= SAVENAME;
  157. }
  158. }
  159. KASSERT(dnode->tn_type == VDIR);
  160. pnode = dnode->tn_spec.tn_dir.tn_parent;
  161. if (pnode == NULL) {
  162. error = ENOENT;
  163. goto out;
  164. }
  165. /*
  166. * Lock the parent tn_nlock before releasing the vnode lock,
  167. * and thus prevents parent from disappearing.
  168. */
  169. rw_enter_write(&pnode->tn_nlock);
  170. VOP_UNLOCK(dvp, 0, curproc);
  171. /*
  172. * Get a vnode of the '..' entry and re-acquire the lock.
  173. * Release the tn_nlock.
  174. */
  175. error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
  176. vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curproc);
  177. goto out;
  178. } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
  179. /*
  180. * Lookup of "." case.
  181. */
  182. if (lastcn && cnp->cn_nameiop == RENAME) {
  183. error = EISDIR;
  184. goto out;
  185. }
  186. vref(dvp);
  187. *vpp = dvp;
  188. error = 0;
  189. goto done;
  190. }
  191. /*
  192. * Other lookup cases: perform directory scan.
  193. */
  194. de = tmpfs_dir_lookup(dnode, cnp);
  195. if (de == NULL) {
  196. /*
  197. * The entry was not found in the directory. This is valid
  198. * if we are creating or renaming an entry and are working
  199. * on the last component of the path name.
  200. */
  201. if (lastcn && (cnp->cn_nameiop == CREATE ||
  202. cnp->cn_nameiop == RENAME)) {
  203. error = VOP_ACCESS(dvp, VWRITE, cred, curproc);
  204. if (error) {
  205. goto out;
  206. }
  207. /*
  208. * We are creating an entry in the file system, so
  209. * save its name for further use by tmpfs_create().
  210. */
  211. cnp->cn_flags |= SAVENAME;
  212. error = EJUSTRETURN;
  213. } else {
  214. error = ENOENT;
  215. }
  216. goto done;
  217. }
  218. tnode = de->td_node;
  219. /*
  220. * If it is not the last path component and found a non-directory
  221. * or non-link entry (which may itself be pointing to a directory),
  222. * raise an error.
  223. */
  224. if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
  225. error = ENOTDIR;
  226. goto out;
  227. }
  228. /* Check the permissions. */
  229. if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  230. error = VOP_ACCESS(dvp, VWRITE, cred, curproc);
  231. if (error)
  232. goto out;
  233. /*
  234. * If not root and directory is sticky, check for permission
  235. * on directory or on file. This implements append-only
  236. * directories.
  237. */
  238. if ((dnode->tn_mode & S_ISTXT) != 0) {
  239. if (cred->cr_uid != 0 &&
  240. cred->cr_uid != dnode->tn_uid &&
  241. cred->cr_uid != tnode->tn_uid) {
  242. error = EPERM;
  243. goto out;
  244. }
  245. }
  246. /*
  247. * XXX pedro: We might need cn_nameptr later in tmpfs_remove()
  248. * or tmpfs_rmdir() for a tmpfs_dir_lookup(). We should really
  249. * get rid of SAVENAME at some point.
  250. */
  251. if (cnp->cn_nameiop == DELETE)
  252. cnp->cn_flags |= SAVENAME;
  253. }
  254. /* Get a vnode for the matching entry. */
  255. rw_enter_write(&tnode->tn_nlock);
  256. error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
  257. done:
  258. /*
  259. * Cache the result, unless request was for creation (as it does
  260. * not improve the performance).
  261. */
  262. if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
  263. cache_enter(dvp, *vpp, cnp);
  264. }
  265. out:
  266. /*
  267. * If (1) we succeded, (2) found a distinct vnode to return and (3) were
  268. * either explicitely told to keep the parent locked or are in the
  269. * middle of a lookup, unlock the parent vnode.
  270. */
  271. if ((error == 0 || error == EJUSTRETURN) && /* (1) */
  272. *vpp != dvp && /* (2) */
  273. (!lockparent || !lastcn)) { /* (3) */
  274. VOP_UNLOCK(dvp, 0, curproc);
  275. cnp->cn_flags |= PDIRUNLOCK;
  276. } else
  277. KASSERT(VOP_ISLOCKED(dvp));
  278. KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
  279. return error;
  280. }
  281. int
  282. tmpfs_create(void *v)
  283. {
  284. struct vop_create_args /* {
  285. struct vnode *a_dvp;
  286. struct vnode **a_vpp;
  287. struct componentname *a_cnp;
  288. struct vattr *a_vap;
  289. } */ *ap = v;
  290. struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
  291. struct componentname *cnp = ap->a_cnp;
  292. struct vattr *vap = ap->a_vap;
  293. KASSERT(VOP_ISLOCKED(dvp));
  294. KASSERT(cnp->cn_flags & HASBUF);
  295. KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
  296. return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  297. }
  298. int
  299. tmpfs_mknod(void *v)
  300. {
  301. struct vop_mknod_args /* {
  302. struct vnode *a_dvp;
  303. struct vnode **a_vpp;
  304. struct componentname *a_cnp;
  305. struct vattr *a_vap;
  306. } */ *ap = v;
  307. struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
  308. struct componentname *cnp = ap->a_cnp;
  309. struct vattr *vap = ap->a_vap;
  310. enum vtype vt = vap->va_type;
  311. int error;
  312. if (vt != VBLK && vt != VCHR && vt != VFIFO) {
  313. vput(dvp);
  314. return EINVAL;
  315. }
  316. /* tmpfs_alloc_file() will unlock 'dvp'. */
  317. error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  318. if (error)
  319. return error;
  320. vput(*vpp);
  321. return 0;
  322. }
  323. int
  324. tmpfs_open(void *v)
  325. {
  326. struct vop_open_args /* {
  327. struct vnode *a_vp;
  328. int a_mode;
  329. kauth_cred_t a_cred;
  330. } */ *ap = v;
  331. struct vnode *vp = ap->a_vp;
  332. mode_t mode = ap->a_mode;
  333. tmpfs_node_t *node;
  334. KASSERT(VOP_ISLOCKED(vp));
  335. node = VP_TO_TMPFS_NODE(vp);
  336. if (node->tn_links < 1) {
  337. /*
  338. * The file is still active, but all its names have been
  339. * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened
  340. * any more, as it is about to be destroyed.
  341. */
  342. return ENOENT;
  343. }
  344. /* If the file is marked append-only, deny write requests. */
  345. if ((node->tn_flags & APPEND) != 0 &&
  346. (mode & (FWRITE | O_APPEND)) == FWRITE) {
  347. return EPERM;
  348. }
  349. return 0;
  350. }
  351. int
  352. tmpfs_close(void *v)
  353. {
  354. struct vop_close_args /* {
  355. struct vnode *a_vp;
  356. int a_fflag;
  357. kauth_cred_t a_cred;
  358. } */ *ap = v;
  359. struct vnode *vp = ap->a_vp;
  360. KASSERT(VOP_ISLOCKED(vp));
  361. return 0;
  362. }
  363. int
  364. tmpfs_access(void *v)
  365. {
  366. struct vop_access_args /* {
  367. struct vnode *a_vp;
  368. int a_mode;
  369. kauth_cred_t a_cred;
  370. } */ *ap = v;
  371. struct vnode *vp = ap->a_vp;
  372. mode_t mode = ap->a_mode;
  373. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  374. const int writing = (mode & VWRITE) != 0;
  375. KASSERT(VOP_ISLOCKED(vp));
  376. /* Possible? */
  377. switch (vp->v_type) {
  378. case VDIR:
  379. case VLNK:
  380. case VREG:
  381. if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
  382. return EROFS;
  383. }
  384. break;
  385. case VBLK:
  386. case VCHR:
  387. case VSOCK:
  388. case VFIFO:
  389. break;
  390. default:
  391. return EINVAL;
  392. }
  393. if (writing && (node->tn_flags & IMMUTABLE) != 0) {
  394. return EPERM;
  395. }
  396. return (vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
  397. mode, ap->a_cred));
  398. }
  399. int
  400. tmpfs_getattr(void *v)
  401. {
  402. struct vop_getattr_args /* {
  403. struct vnode *a_vp;
  404. struct vattr *a_vap;
  405. kauth_cred_t a_cred;
  406. } */ *ap = v;
  407. struct vnode *vp = ap->a_vp;
  408. struct vattr *vap = ap->a_vap;
  409. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  410. vattr_null(vap);
  411. vap->va_type = vp->v_type;
  412. vap->va_mode = node->tn_mode;
  413. vap->va_nlink = node->tn_links;
  414. vap->va_uid = node->tn_uid;
  415. vap->va_gid = node->tn_gid;
  416. vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  417. vap->va_fileid = node->tn_id;
  418. vap->va_size = node->tn_size;
  419. vap->va_blocksize = PAGE_SIZE;
  420. vap->va_atime = node->tn_atime;
  421. vap->va_mtime = node->tn_mtime;
  422. vap->va_ctime = node->tn_ctime;
  423. /* vap->va_birthtime = node->tn_birthtime; */
  424. vap->va_gen = TMPFS_NODE_GEN(node);
  425. vap->va_flags = node->tn_flags;
  426. vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
  427. node->tn_spec.tn_dev.tn_rdev : VNOVAL;
  428. vap->va_bytes = round_page(node->tn_size);
  429. vap->va_filerev = VNOVAL;
  430. vap->va_vaflags = 0;
  431. vap->va_spare = VNOVAL; /* XXX */
  432. return 0;
  433. }
  434. #define GOODTIME(tv) ((tv)->tv_nsec != VNOVAL)
  435. /* XXX Should this operation be atomic? I think it should, but code in
  436. * XXX other places (e.g., ufs) doesn't seem to be... */
  437. int
  438. tmpfs_setattr(void *v)
  439. {
  440. struct vop_setattr_args /* {
  441. struct vnode *a_vp;
  442. struct vattr *a_vap;
  443. kauth_cred_t a_cred;
  444. } */ *ap = v;
  445. struct vnode *vp = ap->a_vp;
  446. struct vattr *vap = ap->a_vap;
  447. struct ucred *cred = ap->a_cred;
  448. struct proc *p = curproc;
  449. int error = 0;
  450. KASSERT(VOP_ISLOCKED(vp));
  451. /* Abort if any unsettable attribute is given. */
  452. if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
  453. vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
  454. vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
  455. vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
  456. vap->va_bytes != VNOVAL) {
  457. return EINVAL;
  458. }
  459. if (error == 0 && (vap->va_flags != VNOVAL))
  460. error = tmpfs_chflags(vp, vap->va_flags, cred, p);
  461. if (error == 0 && (vap->va_size != VNOVAL))
  462. error = tmpfs_chsize(vp, vap->va_size, cred, p);
  463. if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
  464. error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
  465. if (error == 0 && (vap->va_mode != VNOVAL))
  466. error = tmpfs_chmod(vp, vap->va_mode, cred, p);
  467. if (error == 0 && ((vap->va_vaflags & VA_UTIMES_CHANGE)
  468. || GOODTIME(&vap->va_atime)
  469. || GOODTIME(&vap->va_mtime)))
  470. error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
  471. vap->va_vaflags, cred, p);
  472. return error;
  473. }
  474. int
  475. tmpfs_read(void *v)
  476. {
  477. struct vop_read_args /* {
  478. struct vnode *a_vp;
  479. struct uio *a_uio;
  480. int a_ioflag;
  481. struct ucred *a_cred;
  482. } */ *ap = v;
  483. struct vnode *vp = ap->a_vp;
  484. struct uio *uio = ap->a_uio;
  485. /* const int ioflag = ap->a_ioflag; */
  486. tmpfs_node_t *node;
  487. int error;
  488. KASSERT(VOP_ISLOCKED(vp));
  489. if (vp->v_type != VREG) {
  490. return EISDIR;
  491. }
  492. if (uio->uio_offset < 0) {
  493. return EINVAL;
  494. }
  495. node = VP_TO_TMPFS_NODE(vp);
  496. tmpfs_update(node, TMPFS_NODE_ACCESSED);
  497. error = 0;
  498. while (error == 0 && uio->uio_resid > 0) {
  499. vsize_t len;
  500. if (node->tn_size <= uio->uio_offset) {
  501. break;
  502. }
  503. len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
  504. if (len == 0) {
  505. break;
  506. }
  507. error = tmpfs_uiomove(node, uio, len);
  508. }
  509. return error;
  510. }
  511. int
  512. tmpfs_write(void *v)
  513. {
  514. struct vop_write_args /* {
  515. struct vnode *a_vp;
  516. struct uio *a_uio;
  517. int a_ioflag;
  518. kauth_cred_t a_cred;
  519. } */ *ap = v;
  520. struct vnode *vp = ap->a_vp;
  521. struct uio *uio = ap->a_uio;
  522. const int ioflag = ap->a_ioflag;
  523. tmpfs_node_t *node;
  524. off_t oldsize;
  525. ssize_t overrun;
  526. int extended;
  527. int error;
  528. KASSERT(VOP_ISLOCKED(vp));
  529. node = VP_TO_TMPFS_NODE(vp);
  530. oldsize = node->tn_size;
  531. if (vp->v_type != VREG)
  532. return (EINVAL);
  533. if (uio->uio_resid == 0)
  534. return (0);
  535. if (ioflag & IO_APPEND) {
  536. uio->uio_offset = node->tn_size;
  537. }
  538. if (uio->uio_offset < 0 ||
  539. (u_int64_t)uio->uio_offset + uio->uio_resid > LLONG_MAX)
  540. return (EFBIG);
  541. /* do the filesize rlimit check */
  542. if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
  543. return (error);
  544. extended = uio->uio_offset + uio->uio_resid > node->tn_size;
  545. if (extended) {
  546. error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
  547. if (error)
  548. goto out;
  549. }
  550. error = 0;
  551. while (error == 0 && uio->uio_resid > 0) {
  552. vsize_t len;
  553. len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
  554. if (len == 0) {
  555. break;
  556. }
  557. error = tmpfs_uiomove(node, uio, len);
  558. }
  559. if (error) {
  560. (void)tmpfs_reg_resize(vp, oldsize);
  561. }
  562. tmpfs_update(node, TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
  563. (extended ? TMPFS_NODE_CHANGED : 0));
  564. if (extended)
  565. VN_KNOTE(vp, NOTE_WRITE | NOTE_EXTEND);
  566. else
  567. VN_KNOTE(vp, NOTE_WRITE);
  568. out:
  569. if (error) {
  570. KASSERT(oldsize == node->tn_size);
  571. } else {
  572. KASSERT(uio->uio_resid == 0);
  573. /* correct the result for writes clamped by vn_fsizechk() */
  574. uio->uio_resid += overrun;
  575. }
  576. return error;
  577. }
  578. int
  579. tmpfs_fsync(void *v)
  580. {
  581. struct vop_fsync_args /* {
  582. struct vnode *a_vp;
  583. struct ucred *a_cred;
  584. int a_flags;
  585. off_t a_offlo;
  586. off_t a_offhi;
  587. struct lwp *a_l;
  588. } */ *ap = v;
  589. struct vnode *vp = ap->a_vp;
  590. /* Nothing to do. Just update. */
  591. KASSERT(VOP_ISLOCKED(vp));
  592. return 0;
  593. }
  594. /*
  595. * tmpfs_remove: unlink a file.
  596. *
  597. * => Both directory (dvp) and file (vp) are locked.
  598. * => We unlock and drop the reference on both.
  599. */
  600. int
  601. tmpfs_remove(void *v)
  602. {
  603. struct vop_remove_args /* {
  604. struct vnode *a_dvp;
  605. struct vnode *a_vp;
  606. struct componentname *a_cnp;
  607. } */ *ap = v;
  608. struct vnode *dvp = ap->a_dvp, *vp = ap->a_vp;
  609. struct componentname *cnp = ap->a_cnp;
  610. tmpfs_node_t *dnode, *node;
  611. tmpfs_dirent_t *de;
  612. int error;
  613. KASSERT(VOP_ISLOCKED(dvp));
  614. KASSERT(VOP_ISLOCKED(vp));
  615. KASSERT(cnp->cn_flags & HASBUF);
  616. if (vp->v_type == VDIR) {
  617. error = EPERM;
  618. goto out;
  619. }
  620. dnode = VP_TO_TMPFS_NODE(dvp);
  621. node = VP_TO_TMPFS_NODE(vp);
  622. /* Files marked as immutable or append-only cannot be deleted. */
  623. if (node->tn_flags & (IMMUTABLE | APPEND)) {
  624. error = EPERM;
  625. goto out;
  626. }
  627. /*
  628. * Likewise, files residing on directories marked as append-only cannot
  629. * be deleted.
  630. */
  631. if (dnode->tn_flags & APPEND) {
  632. error = EPERM;
  633. goto out;
  634. }
  635. /* Lookup the directory entry (check the cached hint first). */
  636. de = tmpfs_dir_cached(node);
  637. if (de == NULL) {
  638. de = tmpfs_dir_lookup(dnode, cnp);
  639. }
  640. KASSERT(de && de->td_node == node);
  641. /*
  642. * Remove the entry from the directory (drops the link count) and
  643. * destroy it.
  644. * Note: the inode referred by it will not be destroyed
  645. * until the vnode is reclaimed/recycled.
  646. */
  647. tmpfs_dir_detach(dnode, de);
  648. tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
  649. if (node->tn_links > 0) {
  650. /* We removed a hard link. */
  651. tmpfs_update(node, TMPFS_NODE_CHANGED);
  652. }
  653. error = 0;
  654. out:
  655. pool_put(&namei_pool, cnp->cn_pnbuf);
  656. /* Drop the references and unlock the vnodes. */
  657. vput(vp);
  658. if (dvp == vp) {
  659. vrele(dvp);
  660. } else {
  661. vput(dvp);
  662. }
  663. return error;
  664. }
  665. /*
  666. * tmpfs_link: create a hard link.
  667. */
  668. int
  669. tmpfs_link(void *v)
  670. {
  671. struct vop_link_args /* {
  672. struct vnode *a_dvp;
  673. struct vnode *a_vp;
  674. struct componentname *a_cnp;
  675. } */ *ap = v;
  676. struct vnode *dvp = ap->a_dvp;
  677. struct vnode *vp = ap->a_vp;
  678. struct componentname *cnp = ap->a_cnp;
  679. tmpfs_node_t *dnode, *node;
  680. tmpfs_dirent_t *de;
  681. int error;
  682. KASSERT(VOP_ISLOCKED(dvp));
  683. if (vp->v_type == VDIR) {
  684. VOP_ABORTOP(dvp, cnp);
  685. vput(dvp);
  686. return EPERM;
  687. }
  688. KASSERT(dvp != vp);
  689. if (dvp->v_mount != vp->v_mount) {
  690. VOP_ABORTOP(dvp, cnp);
  691. vput(dvp);
  692. return EXDEV;
  693. }
  694. dnode = VP_TO_TMPFS_DIR(dvp);
  695. node = VP_TO_TMPFS_NODE(vp);
  696. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
  697. /* Check for maximum number of links limit. */
  698. if (node->tn_links == LINK_MAX) {
  699. error = EMLINK;
  700. goto out;
  701. }
  702. KASSERT(node->tn_links < LINK_MAX);
  703. /* We cannot create links of files marked immutable or append-only. */
  704. if (node->tn_flags & (IMMUTABLE | APPEND)) {
  705. error = EPERM;
  706. goto out;
  707. }
  708. if (TMPFS_DIRSEQ_FULL(dnode)) {
  709. error = ENOSPC;
  710. goto out;
  711. }
  712. /* Allocate a new directory entry to represent the inode. */
  713. error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
  714. cnp->cn_nameptr, cnp->cn_namelen, &de);
  715. if (error) {
  716. goto out;
  717. }
  718. /*
  719. * Insert the entry into the directory.
  720. * It will increase the inode link count.
  721. */
  722. tmpfs_dir_attach(dnode, de, node);
  723. /* Update the timestamps and trigger the event. */
  724. if (node->tn_vnode) {
  725. VN_KNOTE(node->tn_vnode, NOTE_LINK);
  726. }
  727. tmpfs_update(node, TMPFS_NODE_CHANGED);
  728. error = 0;
  729. out:
  730. pool_put(&namei_pool, cnp->cn_pnbuf);
  731. VOP_UNLOCK(vp, 0, curproc);
  732. vput(dvp);
  733. return error;
  734. }
  735. int
  736. tmpfs_mkdir(void *v)
  737. {
  738. struct vop_mkdir_args /* {
  739. struct vnode *a_dvp;
  740. struct vnode **a_vpp;
  741. struct componentname *a_cnp;
  742. struct vattr *a_vap;
  743. } */ *ap = v;
  744. struct vnode *dvp = ap->a_dvp;
  745. struct vnode **vpp = ap->a_vpp;
  746. struct componentname *cnp = ap->a_cnp;
  747. struct vattr *vap = ap->a_vap;
  748. KASSERT(vap->va_type == VDIR);
  749. return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  750. }
  751. int
  752. tmpfs_rmdir(void *v)
  753. {
  754. struct vop_rmdir_args /* {
  755. struct vnode *a_dvp;
  756. struct vnode *a_vp;
  757. struct componentname *a_cnp;
  758. } */ *ap = v;
  759. struct vnode *dvp = ap->a_dvp;
  760. struct vnode *vp = ap->a_vp;
  761. struct componentname *cnp = ap->a_cnp;
  762. tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
  763. tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
  764. tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
  765. tmpfs_dirent_t *de;
  766. int error = 0;
  767. KASSERT(VOP_ISLOCKED(dvp));
  768. KASSERT(VOP_ISLOCKED(vp));
  769. KASSERT(cnp->cn_flags & HASBUF);
  770. if (cnp->cn_namelen == 2 && cnp->cn_nameptr[0] == '.' &&
  771. cnp->cn_nameptr[1] == '.') {
  772. error = ENOTEMPTY;
  773. goto out;
  774. }
  775. KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
  776. /*
  777. * Directories with more than two entries ('.' and '..') cannot be
  778. * removed.
  779. */
  780. if (node->tn_size > 0) {
  781. KASSERT(error == 0);
  782. TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
  783. error = ENOTEMPTY;
  784. break;
  785. }
  786. if (error)
  787. goto out;
  788. }
  789. /* Lookup the directory entry (check the cached hint first). */
  790. de = tmpfs_dir_cached(node);
  791. if (de == NULL)
  792. de = tmpfs_dir_lookup(dnode, cnp);
  793. KASSERT(de && de->td_node == node);
  794. /* Check flags to see if we are allowed to remove the directory. */
  795. if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
  796. error = EPERM;
  797. goto out;
  798. }
  799. /* Decrement the link count for the virtual '.' entry. */
  800. node->tn_links--;
  801. tmpfs_update(node, TMPFS_NODE_STATUSALL);
  802. /* Detach the directory entry from the directory. */
  803. tmpfs_dir_detach(dnode, de);
  804. /* Purge the cache for parent. */
  805. cache_purge(dvp);
  806. /*
  807. * Destroy the directory entry.
  808. * Note: the inode referred by it will not be destroyed
  809. * until the vnode is reclaimed.
  810. */
  811. tmpfs_free_dirent(tmp, de);
  812. KASSERT(TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir) == NULL);
  813. KASSERT(node->tn_links == 0);
  814. out:
  815. pool_put(&namei_pool, cnp->cn_pnbuf);
  816. /* Release the nodes. */
  817. vput(dvp);
  818. vput(vp);
  819. return error;
  820. }
  821. int
  822. tmpfs_symlink(void *v)
  823. {
  824. struct vop_symlink_args /* {
  825. struct vnode *a_dvp;
  826. struct vnode **a_vpp;
  827. struct componentname *a_cnp;
  828. struct vattr *a_vap;
  829. char *a_target;
  830. } */ *ap = v;
  831. struct vnode *dvp = ap->a_dvp;
  832. struct vnode **vpp = ap->a_vpp;
  833. struct componentname *cnp = ap->a_cnp;
  834. struct vattr *vap = ap->a_vap;
  835. char *target = ap->a_target;
  836. int error;
  837. KASSERT(vap->va_type == 0);
  838. vap->va_type = VLNK;
  839. error = tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
  840. if (error == 0)
  841. vput(*vpp);
  842. return error;
  843. }
  844. int
  845. tmpfs_readdir(void *v)
  846. {
  847. struct vop_readdir_args /* {
  848. struct vnode *a_vp;
  849. struct uio *a_uio;
  850. kauth_cred_t a_cred;
  851. int *a_eofflag;
  852. } */ *ap = v;
  853. struct vnode *vp = ap->a_vp;
  854. struct uio *uio = ap->a_uio;
  855. int *eofflag = ap->a_eofflag;
  856. tmpfs_node_t *node;
  857. int error;
  858. KASSERT(VOP_ISLOCKED(vp));
  859. /* This operation only makes sense on directory nodes. */
  860. if (vp->v_type != VDIR) {
  861. return ENOTDIR;
  862. }
  863. node = VP_TO_TMPFS_DIR(vp);
  864. /*
  865. * Retrieve the directory entries, unless it is being destroyed.
  866. */
  867. if (node->tn_links) {
  868. error = tmpfs_dir_getdents(node, uio);
  869. } else {
  870. error = 0;
  871. }
  872. if (eofflag != NULL) {
  873. *eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF;
  874. }
  875. return error;
  876. }
  877. int
  878. tmpfs_readlink(void *v)
  879. {
  880. struct vop_readlink_args /* {
  881. struct vnode *a_vp;
  882. struct uio *a_uio;
  883. kauth_cred_t a_cred;
  884. } */ *ap = v;
  885. struct vnode *vp = ap->a_vp;
  886. struct uio *uio = ap->a_uio;
  887. tmpfs_node_t *node;
  888. int error;
  889. KASSERT(VOP_ISLOCKED(vp));
  890. KASSERT(uio->uio_offset == 0);
  891. KASSERT(vp->v_type == VLNK);
  892. node = VP_TO_TMPFS_NODE(vp);
  893. error = uiomovei(node->tn_spec.tn_lnk.tn_link,
  894. MIN(node->tn_size, uio->uio_resid), uio);
  895. tmpfs_update(node, TMPFS_NODE_ACCESSED);
  896. return error;
  897. }
  898. int
  899. tmpfs_inactive(void *v)
  900. {
  901. struct vop_inactive_args /* {
  902. struct vnode *a_vp;
  903. int *a_recycle;
  904. } */ *ap = v;
  905. struct vnode *vp = ap->a_vp;
  906. tmpfs_node_t *node;
  907. KASSERT(VOP_ISLOCKED(vp));
  908. node = VP_TO_TMPFS_NODE(vp);
  909. if (vp->v_type == VREG && tmpfs_uio_cached(node))
  910. tmpfs_uio_uncache(node);
  911. VOP_UNLOCK(vp, 0, curproc);
  912. /*
  913. * If we are done with the node, reclaim it so that it can be reused
  914. * immediately.
  915. */
  916. if (node->tn_links == 0)
  917. vrecycle(vp, curproc);
  918. return 0;
  919. }
  920. int
  921. tmpfs_reclaim(void *v)
  922. {
  923. struct vop_reclaim_args /* {
  924. struct vnode *a_vp;
  925. } */ *ap = v;
  926. struct vnode *vp = ap->a_vp;
  927. tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
  928. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  929. int racing;
  930. /* Disassociate inode from vnode. */
  931. rw_enter_write(&node->tn_nlock);
  932. node->tn_vnode = NULL;
  933. vp->v_data = NULL;
  934. /* Check if tmpfs_vnode_get() is racing with us. */
  935. racing = TMPFS_NODE_RECLAIMING(node);
  936. rw_exit_write(&node->tn_nlock);
  937. /*
  938. * If inode is not referenced, i.e. no links, then destroy it.
  939. * Note: if racing - inode is about to get a new vnode, leave it.
  940. */
  941. if (node->tn_links == 0 && !racing) {
  942. tmpfs_free_node(tmp, node);
  943. }
  944. return 0;
  945. }
  946. int
  947. tmpfs_pathconf(void *v)
  948. {
  949. struct vop_pathconf_args /* {
  950. struct vnode *a_vp;
  951. int a_name;
  952. register_t *a_retval;
  953. } */ *ap = v;
  954. const int name = ap->a_name;
  955. register_t *retval = ap->a_retval;
  956. int error = 0;
  957. switch (name) {
  958. case _PC_LINK_MAX:
  959. *retval = LINK_MAX;
  960. break;
  961. case _PC_NAME_MAX:
  962. *retval = TMPFS_MAXNAMLEN;
  963. break;
  964. case _PC_CHOWN_RESTRICTED:
  965. *retval = 1;
  966. break;
  967. case _PC_NO_TRUNC:
  968. *retval = 1;
  969. break;
  970. case _PC_FILESIZEBITS:
  971. *retval = 64;
  972. break;
  973. case _PC_TIMESTAMP_RESOLUTION:
  974. *retval = 1;
  975. break;
  976. default:
  977. error = EINVAL;
  978. }
  979. return error;
  980. }
  981. int
  982. tmpfs_advlock(void *v)
  983. {
  984. struct vop_advlock_args /* {
  985. struct vnode *a_vp;
  986. void * a_id;
  987. int a_op;
  988. struct flock *a_fl;
  989. int a_flags;
  990. } */ *ap = v;
  991. struct vnode *vp = ap->a_vp;
  992. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  993. return lf_advlock(&node->tn_lockf, node->tn_size, ap->a_id, ap->a_op,
  994. ap->a_fl, ap->a_flags);
  995. }
  996. int
  997. tmpfs_print(void *v)
  998. {
  999. struct vop_print_args /* {
  1000. struct vnode *a_vp;
  1001. } */ *ap = v;
  1002. struct vnode *vp = ap->a_vp;
  1003. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  1004. printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
  1005. "\tmode 0%o, owner %d, group %d, size %lld",
  1006. node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
  1007. node->tn_gid, node->tn_size);
  1008. #ifdef FIFO
  1009. if (vp->v_type == VFIFO)
  1010. fifo_printinfo(vp);
  1011. #endif
  1012. printf("\n");
  1013. return 0;
  1014. }
  1015. /* a null op */
  1016. int
  1017. tmpfs_bwrite(void *v)
  1018. {
  1019. return 0;
  1020. }
  1021. int
  1022. tmpfs_poll(void *v)
  1023. {
  1024. struct vop_poll_args *ap = v;
  1025. return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
  1026. }
  1027. int
  1028. tmpfs_strategy(void *v)
  1029. {
  1030. return EOPNOTSUPP;
  1031. }
  1032. int
  1033. tmpfs_ioctl(void *v)
  1034. {
  1035. return ENOTTY;
  1036. }
  1037. int
  1038. tmpfs_lock(void *v)
  1039. {
  1040. struct vop_lock_args *ap = v;
  1041. tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
  1042. return lockmgr(&tnp->tn_vlock, ap->a_flags, NULL);
  1043. }
  1044. int
  1045. tmpfs_unlock(void *v)
  1046. {
  1047. struct vop_unlock_args *ap = v;
  1048. tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
  1049. return lockmgr(&tnp->tn_vlock, ap->a_flags | LK_RELEASE, NULL);
  1050. }
  1051. int
  1052. tmpfs_islocked(void *v)
  1053. {
  1054. struct vop_islocked_args *ap = v;
  1055. tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
  1056. return lockstatus(&tnp->tn_vlock);
  1057. }
  1058. /*
  1059. * tmpfs_rename: rename routine, the hairiest system call, with the
  1060. * insane API.
  1061. *
  1062. * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
  1063. * and tvp (to-leaf), if exists (NULL if not).
  1064. *
  1065. * => Caller holds a reference on fdvp and fvp, they are unlocked.
  1066. * Note: fdvp and fvp can refer to the same object (i.e. when it is root).
  1067. *
  1068. * => Both tdvp and tvp are referenced and locked. It is our responsibility
  1069. * to release the references and unlock them (or destroy).
  1070. */
  1071. /*
  1072. * First, some forward declarations of subroutines.
  1073. */
  1074. int tmpfs_sane_rename(struct vnode *, struct componentname *,
  1075. struct vnode *, struct componentname *, struct ucred *, int);
  1076. int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
  1077. struct ucred *,
  1078. struct vnode *, struct tmpfs_node *, struct componentname *,
  1079. struct tmpfs_dirent **, struct vnode **,
  1080. struct vnode *, struct tmpfs_node *, struct componentname *,
  1081. struct tmpfs_dirent **, struct vnode **);
  1082. int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
  1083. struct ucred *,
  1084. struct vnode *, struct tmpfs_node *,
  1085. struct componentname *, struct tmpfs_dirent **, struct vnode **,
  1086. struct componentname *, struct tmpfs_dirent **, struct vnode **);
  1087. int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
  1088. struct ucred *,
  1089. struct vnode *, struct tmpfs_node *, struct componentname *,
  1090. struct tmpfs_dirent **, struct vnode **,
  1091. struct vnode *, struct tmpfs_node *, struct componentname *,
  1092. struct tmpfs_dirent **, struct vnode **);
  1093. void tmpfs_rename_exit(struct tmpfs_mount *,
  1094. struct vnode *, struct vnode *, struct vnode *, struct vnode *);
  1095. int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
  1096. int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
  1097. struct tmpfs_node **);
  1098. int tmpfs_rename_lock(struct mount *, struct ucred *, int,
  1099. struct vnode *, struct tmpfs_node *, struct componentname *, int,
  1100. struct tmpfs_dirent **, struct vnode **,
  1101. struct vnode *, struct tmpfs_node *, struct componentname *, int,
  1102. struct tmpfs_dirent **, struct vnode **);
  1103. void tmpfs_rename_attachdetach(struct tmpfs_mount *,
  1104. struct vnode *, struct tmpfs_dirent *, struct vnode *,
  1105. struct vnode *, struct tmpfs_dirent *, struct vnode *);
  1106. int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
  1107. struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, struct ucred *);
  1108. int tmpfs_rename_check_possible(struct tmpfs_node *,
  1109. struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
  1110. int tmpfs_rename_check_permitted(struct ucred *,
  1111. struct tmpfs_node *, struct tmpfs_node *,
  1112. struct tmpfs_node *, struct tmpfs_node *);
  1113. int tmpfs_remove_check_possible(struct tmpfs_node *,
  1114. struct tmpfs_node *);
  1115. int tmpfs_remove_check_permitted(struct ucred *,
  1116. struct tmpfs_node *, struct tmpfs_node *);
  1117. int tmpfs_check_sticky(struct ucred *,
  1118. struct tmpfs_node *, struct tmpfs_node *);
  1119. void tmpfs_rename_cache_purge(struct vnode *, struct vnode *, struct vnode *,
  1120. struct vnode *);
  1121. void tmpfs_rename_abort(void *);
  1122. int
  1123. tmpfs_rename(void *v)
  1124. {
  1125. struct vop_rename_args /* {
  1126. struct vnode *a_fdvp;
  1127. struct vnode *a_fvp;
  1128. struct componentname *a_fcnp;
  1129. struct vnode *a_tdvp;
  1130. struct vnode *a_tvp;
  1131. struct componentname *a_tcnp;
  1132. } */ *ap = v;
  1133. struct vnode *fdvp = ap->a_fdvp;
  1134. struct vnode *fvp = ap->a_fvp;
  1135. struct componentname *fcnp = ap->a_fcnp;
  1136. struct vnode *tdvp = ap->a_tdvp;
  1137. struct vnode *tvp = ap->a_tvp;
  1138. struct componentname *tcnp = ap->a_tcnp;
  1139. struct ucred *cred;
  1140. int error;
  1141. KASSERT(fdvp != NULL);
  1142. KASSERT(fvp != NULL);
  1143. KASSERT(fcnp != NULL);
  1144. KASSERT(fcnp->cn_nameptr != NULL);
  1145. KASSERT(tdvp != NULL);
  1146. KASSERT(tcnp != NULL);
  1147. KASSERT(fcnp->cn_nameptr != NULL);
  1148. /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
  1149. /* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
  1150. KASSERT(fdvp->v_type == VDIR);
  1151. KASSERT(tdvp->v_type == VDIR);
  1152. KASSERT(fcnp->cn_flags & HASBUF);
  1153. KASSERT(tcnp->cn_flags & HASBUF);
  1154. cred = fcnp->cn_cred;
  1155. KASSERT(tcnp->cn_cred == cred);
  1156. /*
  1157. * Check for cross-device rename.
  1158. */
  1159. if (fvp->v_mount != tdvp->v_mount ||
  1160. (tvp != NULL && (fvp->v_mount != tvp->v_mount))) {
  1161. tmpfs_rename_abort(v);
  1162. return EXDEV;
  1163. }
  1164. /*
  1165. * Can't check the locks on these until we know they're on
  1166. * the same FS, as not all FS do locking the same way.
  1167. */
  1168. KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
  1169. KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
  1170. /*
  1171. * Reject renaming '.' and '..'.
  1172. */
  1173. if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
  1174. (fcnp->cn_namelen == 2 && fcnp->cn_nameptr[0] == '.' &&
  1175. fcnp->cn_nameptr[1] == '.')) {
  1176. tmpfs_rename_abort(v);
  1177. return EINVAL;
  1178. }
  1179. /*
  1180. * Sanitize our world from the VFS insanity. Unlock the target
  1181. * directory and node, which are locked. Release the children,
  1182. * which are referenced. Check for rename("x", "y/."), which
  1183. * it is our responsibility to reject, not the caller's. (But
  1184. * the caller does reject rename("x/.", "y"). Go figure.)
  1185. */
  1186. VOP_UNLOCK(tdvp, 0, curproc);
  1187. if ((tvp != NULL) && (tvp != tdvp))
  1188. VOP_UNLOCK(tvp, 0, curproc);
  1189. vrele(fvp);
  1190. if (tvp != NULL)
  1191. vrele(tvp);
  1192. if (tvp == tdvp) {
  1193. error = EINVAL;
  1194. goto out;
  1195. }
  1196. error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, 0);
  1197. out: /*
  1198. * All done, whether with success or failure. Release the
  1199. * directory nodes now, as the caller expects from the VFS
  1200. * protocol.
  1201. */
  1202. vrele(fdvp);
  1203. vrele(tdvp);
  1204. return error;
  1205. }
  1206. /*
  1207. * tmpfs_sane_rename: rename routine, the hairiest system call, with
  1208. * the sane API.
  1209. *
  1210. * Arguments:
  1211. *
  1212. * . fdvp (from directory vnode),
  1213. * . fcnp (from component name),
  1214. * . tdvp (to directory vnode), and
  1215. * . tcnp (to component name).
  1216. *
  1217. * fdvp and tdvp must be referenced and unlocked.
  1218. */
  1219. int
  1220. tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
  1221. struct vnode *tdvp, struct componentname *tcnp, struct ucred *cred,
  1222. int posixly_correct)
  1223. {
  1224. struct mount *mount;
  1225. struct tmpfs_mount *tmpfs;
  1226. struct tmpfs_node *fdnode, *tdnode;
  1227. struct tmpfs_dirent *fde, *tde;
  1228. struct vnode *fvp, *tvp;
  1229. char *newname;
  1230. int error;
  1231. KASSERT(fdvp != NULL);
  1232. KASSERT(fcnp != NULL);
  1233. KASSERT(tdvp != NULL);
  1234. KASSERT(tcnp != NULL);
  1235. /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
  1236. /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
  1237. KASSERT(fdvp->v_type == VDIR);
  1238. KASSERT(tdvp->v_type == VDIR);
  1239. KASSERT(fdvp->v_mount == tdvp->v_mount);
  1240. KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
  1241. KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
  1242. KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
  1243. KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
  1244. KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
  1245. (fcnp->cn_nameptr[1] != '.'));
  1246. KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
  1247. (tcnp->cn_nameptr[1] != '.'));
  1248. /*
  1249. * Pull out the tmpfs data structures.
  1250. */
  1251. fdnode = VP_TO_TMPFS_NODE(fdvp);
  1252. tdnode = VP_TO_TMPFS_NODE(tdvp);
  1253. KASSERT(fdnode != NULL);
  1254. KASSERT(tdnode != NULL);
  1255. KASSERT(fdnode->tn_vnode == fdvp);
  1256. KASSERT(tdnode->tn_vnode == tdvp);
  1257. KASSERT(fdnode->tn_type == VDIR);
  1258. KASSERT(tdnode->tn_type == VDIR);
  1259. mount = fdvp->v_mount;
  1260. KASSERT(mount != NULL);
  1261. KASSERT(mount == tdvp->v_mount);
  1262. /* XXX How can we be sure this stays true? (Not that you're
  1263. * likely to mount a tmpfs read-only...) */
  1264. KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
  1265. tmpfs = VFS_TO_TMPFS(mount);
  1266. KASSERT(tmpfs != NULL);
  1267. /*
  1268. * Decide whether we need a new name, and allocate memory for
  1269. * it if so. Do this before locking anything or taking
  1270. * destructive actions so that we can back out safely and sleep
  1271. * safely. XXX Is sleeping an issue here? Can this just be
  1272. * moved into tmpfs_rename_attachdetach?
  1273. */
  1274. if (tmpfs_strname_neqlen(fcnp, tcnp)) {
  1275. newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
  1276. if (newname == NULL) {
  1277. error = ENOSPC;
  1278. goto out_unlocked;
  1279. }
  1280. } else {
  1281. newname = NULL;
  1282. }
  1283. /*
  1284. * Lock and look up everything. GCC is not very clever.
  1285. */
  1286. fde = tde = NULL;
  1287. fvp = tvp = NULL;
  1288. error = tmpfs_rename_enter(mount, tmpfs, cred,
  1289. fdvp, fdnode, fcnp, &fde, &fvp,
  1290. tdvp, tdnode, tcnp, &tde, &tvp);
  1291. if (error)
  1292. goto out_unlocked;
  1293. /*
  1294. * Check that everything is locked and looks right.
  1295. */
  1296. KASSERT(fde != NULL);
  1297. KASSERT(fvp != NULL);
  1298. KASSERT(fde->td_node != NULL);
  1299. KASSERT(fde->td_node->tn_vnode == fvp);
  1300. KASSERT(fde->td_node->tn_type == fvp->v_type);
  1301. KASSERT((tde == NULL) == (tvp == NULL));
  1302. KASSERT((tde == NULL) || (tde->td_node != NULL));
  1303. KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
  1304. KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
  1305. KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
  1306. KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
  1307. KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
  1308. KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
  1309. /*
  1310. * If the source and destination are the same object, we need
  1311. * only at most delete the source entry.
  1312. */
  1313. if (fvp == tvp) {
  1314. KASSERT(tvp != NULL);
  1315. if (fde->td_node->tn_type == VDIR) {
  1316. /* XXX How can this possibly happen? */
  1317. error = EINVAL;
  1318. goto out_locked;
  1319. }
  1320. if (!posixly_correct && (fde != tde)) {
  1321. /* XXX Doesn't work because of locking.
  1322. * error = VOP_REMOVE(fdvp, fvp);
  1323. */
  1324. error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
  1325. cred);
  1326. if (error)
  1327. goto out_locked;
  1328. }
  1329. goto success;
  1330. }
  1331. KASSERT(fde != tde);
  1332. KASSERT(fvp != tvp);
  1333. /*
  1334. * If the target exists, refuse to rename a directory over a
  1335. * non-directory or vice versa, or to clobber a non-empty
  1336. * directory.
  1337. */
  1338. if (tvp != NULL) {
  1339. KASSERT(tde != NULL);
  1340. KASSERT(tde->td_node != NULL);
  1341. if (fvp->v_type == VDIR && tvp->v_type == VDIR)
  1342. error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
  1343. else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
  1344. error = ENOTDIR;
  1345. else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
  1346. error = EISDIR;
  1347. else
  1348. error = 0;
  1349. if (error)
  1350. goto out_locked;
  1351. KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
  1352. }
  1353. /*
  1354. * Authorize the rename.
  1355. */
  1356. error = tmpfs_rename_check_possible(fdnode, fde->td_node,
  1357. tdnode, (tde? tde->td_node : NULL));
  1358. if (error)
  1359. goto out_locked;
  1360. error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
  1361. tdnode, (tde? tde->td_node : NULL));
  1362. if (error)
  1363. goto out_locked;
  1364. /*
  1365. * Everything is hunky-dory. Shuffle the directory entries.
  1366. */
  1367. tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
  1368. /*
  1369. * Update the directory entry's name necessary, and flag
  1370. * metadata updates. A memory allocation failure here is not
  1371. * OK because we've already committed some changes that we
  1372. * can't back out at this point, and we have things locked so
  1373. * we can't sleep, hence the early allocation above.
  1374. */
  1375. if (newname != NULL) {
  1376. KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
  1377. tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
  1378. fde->td_namelen = (uint16_t)tcnp->cn_namelen;
  1379. (void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
  1380. /* Commit newname and don't free it on the way out. */
  1381. fde->td_name = newname;
  1382. newname = NULL;
  1383. tmpfs_update(fde->td_node, TMPFS_NODE_CHANGED);
  1384. tmpfs_update(tdnode, TMPFS_NODE_MODIFIED);
  1385. }
  1386. success:
  1387. VN_KNOTE(fvp, NOTE_RENAME);
  1388. tmpfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
  1389. error = 0;
  1390. out_locked:
  1391. tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
  1392. out_unlocked:
  1393. /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
  1394. /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
  1395. /* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
  1396. /* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
  1397. if (newname != NULL)
  1398. tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
  1399. return error;
  1400. }
  1401. /*
  1402. * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
  1403. * and the associated vnode in fvp_ret; fail if not found. Look up
  1404. * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
  1405. * associated vnode in tvp_ret; store null instead if not found. Fail
  1406. * if anything has been mounted on any of the nodes involved.
  1407. *
  1408. * fdvp and tdvp must be referenced.
  1409. *
  1410. * On entry, nothing is locked.
  1411. *
  1412. * On success, everything is locked, and *fvp_ret, and *tvp_ret if
  1413. * nonnull, are referenced. The only pairs of vnodes that may be
  1414. * identical are {fdvp, tdvp} and {fvp, tvp}.
  1415. *
  1416. * On failure, everything remains as was.
  1417. *
  1418. * Locking everything including the source and target nodes is
  1419. * necessary to make sure that, e.g., link count updates are OK. The
  1420. * locking order is, in general, ancestor-first, matching the order you
  1421. * need to use to look up a descendant anyway.
  1422. */
  1423. int
  1424. tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
  1425. struct ucred *cred,
  1426. struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
  1427. struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
  1428. struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
  1429. struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
  1430. {
  1431. int error;
  1432. KASSERT(mount != NULL);
  1433. KASSERT(tmpfs != NULL);
  1434. KASSERT(fdvp != NULL);
  1435. KASSERT(fdnode != NULL);
  1436. KASSERT(fcnp != NULL);
  1437. KASSERT(fde_ret != NULL);
  1438. KASSERT(fvp_ret != NULL);
  1439. KASSERT(tdvp != NULL);
  1440. KASSERT(tdnode != NULL);
  1441. KASSERT(tcnp != NULL);
  1442. KASSERT(tde_ret != NULL);
  1443. KASSERT(tvp_ret != NULL);
  1444. KASSERT(fdnode->tn_vnode == fdvp);
  1445. KASSERT(tdnode->tn_vnode == tdvp);
  1446. KASSERT(fdnode->tn_type == VDIR);
  1447. KASSERT(tdnode->tn_type == VDIR);
  1448. if (fdvp == tdvp) {
  1449. KASSERT(fdnode == tdnode);
  1450. error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
  1451. fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
  1452. } else {
  1453. KASSERT(fdnode != tdnode);
  1454. error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
  1455. fdvp, fdnode, fcnp, fde_ret, fvp_ret,
  1456. tdvp, tdnode, tcnp, tde_ret, tvp_ret);
  1457. }
  1458. if (error)
  1459. return error;
  1460. KASSERT(*fde_ret != NULL);
  1461. KASSERT(*fvp_ret != NULL);
  1462. KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
  1463. KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
  1464. KASSERT((*tde_ret == NULL) ||
  1465. ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
  1466. KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
  1467. KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
  1468. KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
  1469. KASSERT((*tvp_ret == NULL) ||
  1470. (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
  1471. KASSERT(*fvp_ret != fdvp);
  1472. KASSERT(*fvp_ret != tdvp);
  1473. KASSERT(*tvp_ret != fdvp);
  1474. KASSERT(*tvp_ret != tdvp);
  1475. return 0;
  1476. }
  1477. /*
  1478. * Lock and look up with a common source/target directory.
  1479. */
  1480. int
  1481. tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
  1482. struct ucred *cred,
  1483. struct vnode *dvp, struct tmpfs_node *dnode,
  1484. struct componentname *fcnp,
  1485. struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
  1486. struct componentname *tcnp,
  1487. struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
  1488. {
  1489. struct tmpfs_dirent *fde, *tde;
  1490. struct vnode *fvp, *tvp;
  1491. int error;
  1492. error = tmpfs_rename_lock_directory(dvp, dnode);
  1493. if (error)
  1494. goto fail0;
  1495. /* Did we lose a race with mount? */
  1496. if (dvp->v_mountedhere != NULL) {
  1497. error = EBUSY;
  1498. goto fail1;
  1499. }
  1500. /* Make sure the caller may read the directory. */
  1501. error = VOP_ACCESS(dvp, VEXEC, cred, curproc);
  1502. if (error)
  1503. goto fail1;
  1504. /*
  1505. * The order in which we lock the source and target nodes is
  1506. * irrelevant because there can only be one rename on this
  1507. * directory in flight at a time, and we have it locked.
  1508. */
  1509. fde = tmpfs_dir_lookup(dnode, fcnp);
  1510. if (fde == NULL) {
  1511. error = ENOENT;
  1512. goto fail1;
  1513. }
  1514. KASSERT(fde->td_node != NULL);
  1515. /* We ruled out `.' earlier. */
  1516. KASSERT(fde->td_node != dnode);
  1517. /* We ruled out `..' earlier. */
  1518. KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
  1519. rw_enter_write(&fde->td_node->tn_nlock);
  1520. error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
  1521. if (error)
  1522. goto fail1;
  1523. KASSERT(fvp != NULL);
  1524. KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
  1525. KASSERT(fvp != dvp);
  1526. KASSERT(fvp->v_mount == mount);
  1527. /* Refuse to rename a mount point. */
  1528. if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
  1529. error = EBUSY;
  1530. goto fail2;
  1531. }
  1532. tde = tmpfs_dir_lookup(dnode, tcnp);
  1533. if (tde == NULL) {
  1534. tvp = NULL;
  1535. } else {
  1536. KASSERT(tde->td_node != NULL);
  1537. /* We ruled out `.' earlier. */
  1538. KASSERT(tde->td_node != dnode);
  1539. /* We ruled out `..' earlier. */
  1540. KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
  1541. if (tde->td_node != fde->td_node) {
  1542. rw_enter_write(&tde->td_node->tn_nlock);
  1543. error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
  1544. if (error)
  1545. goto fail2;
  1546. KASSERT(tvp->v_mount == mount);
  1547. /* Refuse to rename over a mount point. */
  1548. if ((tvp->v_type == VDIR) &&
  1549. (tvp->v_mountedhere != NULL)) {
  1550. error = EBUSY;
  1551. goto fail3;
  1552. }
  1553. } else {
  1554. tvp = fvp;
  1555. vref(tvp);
  1556. }
  1557. KASSERT(tvp != NULL);
  1558. KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
  1559. }
  1560. KASSERT(tvp != dvp);
  1561. *fde_ret = fde;
  1562. *fvp_ret = fvp;
  1563. *tde_ret = tde;
  1564. *tvp_ret = tvp;
  1565. return 0;
  1566. fail3: if (tvp != NULL) {
  1567. if (tvp != fvp)
  1568. vput(tvp);
  1569. else
  1570. vrele(tvp);
  1571. }
  1572. fail2: vput(fvp);
  1573. fail1: VOP_UNLOCK(dvp, 0, curproc);
  1574. fail0: return error;
  1575. }
  1576. /*
  1577. * Lock and look up with separate source and target directories.
  1578. */
  1579. int
  1580. tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
  1581. struct ucred *cred,
  1582. struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
  1583. struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
  1584. struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
  1585. struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
  1586. {
  1587. struct tmpfs_node *intermediate_node;
  1588. struct tmpfs_dirent *fde, *tde;
  1589. struct vnode *fvp, *tvp;
  1590. int error;
  1591. KASSERT(fdvp != tdvp);
  1592. KASSERT(fdnode != tdnode);
  1593. #if 0 /* XXX */
  1594. mutex_enter(&tmpfs->tm_rename_lock);
  1595. #endif
  1596. error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
  1597. if (error)
  1598. goto fail;
  1599. /*
  1600. * intermediate_node == NULL means fdnode is not an ancestor of
  1601. * tdnode.
  1602. */
  1603. if (intermediate_node == NULL)
  1604. error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
  1605. tdvp, tdnode, tcnp, 1, &tde, &tvp,
  1606. fdvp, fdnode, fcnp, 0, &fde, &fvp);
  1607. else
  1608. error = tmpfs_rename_lock(mount, cred, EINVAL,
  1609. fdvp, fdnode, fcnp, 0, &fde, &fvp,
  1610. tdvp, tdnode, tcnp, 1, &tde, &tvp);
  1611. if (error)
  1612. goto fail;
  1613. KASSERT(fde != NULL);
  1614. KASSERT(fde->td_node != NULL);
  1615. /*
  1616. * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
  1617. */
  1618. if (fde->td_node == intermediate_node) {
  1619. tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
  1620. return EINVAL;
  1621. }
  1622. *fde_ret = fde;
  1623. *fvp_ret = fvp;
  1624. *tde_ret = tde;
  1625. *tvp_ret = tvp;
  1626. return 0;
  1627. fail:
  1628. #if 0 /* XXX */
  1629. mutex_exit(&tmpfs->tm_rename_lock);
  1630. #endif
  1631. return error;
  1632. }
  1633. /*
  1634. * Unlock everything we locked for rename.
  1635. *
  1636. * fdvp and tdvp must be referenced.
  1637. *
  1638. * On entry, everything is locked, and fvp and tvp referenced.
  1639. *
  1640. * On exit, everything is unlocked, and fvp and tvp are released.
  1641. */
  1642. void
  1643. tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
  1644. struct vnode *fdvp, struct vnode *fvp,
  1645. struct vnode *tdvp, struct vnode *tvp)
  1646. {
  1647. KASSERT(tmpfs != NULL);
  1648. KASSERT(fdvp != NULL);
  1649. KASSERT(fvp != NULL);
  1650. KASSERT(fdvp != fvp);
  1651. KASSERT(fdvp != tvp);
  1652. KASSERT(tdvp != tvp);
  1653. KASSERT(tdvp != fvp);
  1654. KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
  1655. KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
  1656. KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
  1657. KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
  1658. if (tvp != NULL) {
  1659. if (tvp != fvp)
  1660. vput(tvp);
  1661. else
  1662. vrele(tvp);
  1663. }
  1664. VOP_UNLOCK(tdvp, 0, curproc);
  1665. vput(fvp);
  1666. if (fdvp != tdvp)
  1667. VOP_UNLOCK(fdvp, 0, curproc);
  1668. #if 0 /* XXX */
  1669. if (fdvp != tdvp)
  1670. mutex_exit(&tmpfs->tm_rename_lock);
  1671. #endif
  1672. }
  1673. /*
  1674. * Lock a directory, but fail if it has been rmdir'd.
  1675. *
  1676. * vp must be referenced.
  1677. */
  1678. int
  1679. tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
  1680. {
  1681. KASSERT(vp != NULL);
  1682. KASSERT(node != NULL);
  1683. KASSERT(node->tn_vnode == vp);
  1684. KASSERT(node->tn_type == VDIR);
  1685. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
  1686. if (node->tn_spec.tn_dir.tn_parent == NULL) {
  1687. VOP_UNLOCK(vp, 0, curproc);
  1688. return ENOENT;
  1689. }
  1690. return 0;
  1691. }
  1692. /*
  1693. * Analyze the genealogy of the source and target nodes.
  1694. *
  1695. * On success, stores in *intermediate_node_ret either the child of
  1696. * fdnode of which tdnode is a descendant, or null if tdnode is not a
  1697. * descendant of fdnode at all.
  1698. *
  1699. * fdnode and tdnode must be unlocked and referenced. The file
  1700. * system's rename lock must also be held, to exclude concurrent
  1701. * changes to the file system's genealogy other than rmdir.
  1702. *
  1703. * XXX This causes an extra lock/unlock of tdnode in the case when
  1704. * we're just about to lock it again before locking anything else.
  1705. * However, changing that requires reorganizing the code to make it
  1706. * even more horrifically obscure.
  1707. */
  1708. int
  1709. tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
  1710. struct tmpfs_node **intermediate_node_ret)
  1711. {
  1712. struct tmpfs_node *node = tdnode, *parent;
  1713. int error;
  1714. KASSERT(fdnode != NULL);
  1715. KASSERT(tdnode != NULL);
  1716. KASSERT(fdnode != tdnode);
  1717. KASSERT(intermediate_node_ret != NULL);
  1718. KASSERT(fdnode->tn_vnode != NULL);
  1719. KASSERT(tdnode->tn_vnode != NULL);
  1720. KASSERT(fdnode->tn_type == VDIR);
  1721. KASSERT(tdnode->tn_type == VDIR);
  1722. /*
  1723. * We need to provisionally lock tdnode->tn_vnode to keep rmdir
  1724. * from deleting it -- or any ancestor -- at an inopportune
  1725. * moment.
  1726. */
  1727. error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
  1728. if (error)
  1729. return error;
  1730. for (;;) {
  1731. parent = node->tn_spec.tn_dir.tn_parent;
  1732. KASSERT(parent != NULL);
  1733. KASSERT(parent->tn_type == VDIR);
  1734. /* Did we hit the root without finding fdnode? */
  1735. if (parent == node) {
  1736. *intermediate_node_ret = NULL;
  1737. break;
  1738. }
  1739. /* Did we find that fdnode is an ancestor? */
  1740. if (parent == fdnode) {
  1741. *intermediate_node_ret = node;
  1742. break;
  1743. }
  1744. /* Neither -- keep ascending the family tree. */
  1745. node = parent;
  1746. }
  1747. VOP_UNLOCK(tdnode->tn_vnode, 0, curproc);
  1748. return 0;
  1749. }
  1750. /*
  1751. * Lock directories a and b, which must be distinct, and look up and
  1752. * lock nodes a and b. Do a first and then b. Directory b may not be
  1753. * an ancestor of directory a, although directory a may be an ancestor
  1754. * of directory b. Fail with overlap_error if node a is directory b.
  1755. * Neither componentname may be `.' or `..'.
  1756. *
  1757. * a_dvp and b_dvp must be referenced.
  1758. *
  1759. * On entry, a_dvp and b_dvp are unlocked.
  1760. *
  1761. * On success,
  1762. * . a_dvp and b_dvp are locked,
  1763. * . *a_dirent_ret is filled with a directory entry whose node is
  1764. * locked and referenced,
  1765. * . *b_vp_ret is filled with the corresponding vnode,
  1766. * . *b_dirent_ret is filled either with null or with a directory entry
  1767. * whose node is locked and referenced,
  1768. * . *b_vp is filled either with null or with the corresponding vnode,
  1769. * and
  1770. * . the only pair of vnodes that may be identical is a_vp and b_vp.
  1771. *
  1772. * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
  1773. * *a_vp, *b_dirent_ret, and *b_vp are left alone.
  1774. */
  1775. int
  1776. tmpfs_rename_lock(struct mount *mount, struct ucred *cred, int overlap_error,
  1777. struct vnode *a_dvp, struct tmpfs_node *a_dnode,
  1778. struct componentname *a_cnp, int a_missing_ok,
  1779. struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
  1780. struct vnode *b_dvp, struct tmpfs_node *b_dnode,
  1781. struct componentname *b_cnp, int b_missing_ok,
  1782. struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
  1783. {
  1784. struct tmpfs_dirent *a_dirent, *b_dirent;
  1785. struct vnode *a_vp, *b_vp;
  1786. int error;
  1787. KASSERT(a_dvp != NULL);
  1788. KASSERT(a_dnode != NULL);
  1789. KASSERT(a_cnp != NULL);
  1790. KASSERT(a_dirent_ret != NULL);
  1791. KASSERT(a_vp_ret != NULL);
  1792. KASSERT(b_dvp != NULL);
  1793. KASSERT(b_dnode != NULL);
  1794. KASSERT(b_cnp != NULL);
  1795. KASSERT(b_dirent_ret != NULL);
  1796. KASSERT(b_vp_ret != NULL);
  1797. KASSERT(a_dvp != b_dvp);
  1798. KASSERT(a_dnode != b_dnode);
  1799. KASSERT(a_dnode->tn_vnode == a_dvp);
  1800. KASSERT(b_dnode->tn_vnode == b_dvp);
  1801. KASSERT(a_dnode->tn_type == VDIR);
  1802. KASSERT(b_dnode->tn_type == VDIR);
  1803. KASSERT(a_missing_ok != b_missing_ok);
  1804. error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
  1805. if (error)
  1806. goto fail0;
  1807. /* Did we lose a race with mount? */
  1808. if (a_dvp->v_mountedhere != NULL) {
  1809. error = EBUSY;
  1810. goto fail1;
  1811. }
  1812. /* Make sure the caller may read the directory. */
  1813. error = VOP_ACCESS(a_dvp, VEXEC, cred, curproc);
  1814. if (error)
  1815. goto fail1;
  1816. a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
  1817. if (a_dirent != NULL) {
  1818. KASSERT(a_dirent->td_node != NULL);
  1819. /* We ruled out `.' earlier. */
  1820. KASSERT(a_dirent->td_node != a_dnode);
  1821. /* We ruled out `..' earlier. */
  1822. KASSERT(a_dirent->td_node !=
  1823. a_dnode->tn_spec.tn_dir.tn_parent);
  1824. if (a_dirent->td_node == b_dnode) {
  1825. error = overlap_error;
  1826. goto fail1;
  1827. }
  1828. rw_enter_write(&a_dirent->td_node->tn_nlock);
  1829. error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
  1830. if (error)
  1831. goto fail1;
  1832. KASSERT(a_vp->v_mount == mount);
  1833. /* Refuse to rename (over) a mount point. */
  1834. if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
  1835. error = EBUSY;
  1836. goto fail2;
  1837. }
  1838. } else if (!a_missing_ok) {
  1839. error = ENOENT;
  1840. goto fail1;
  1841. } else {
  1842. a_vp = NULL;
  1843. }
  1844. KASSERT(a_vp != a_dvp);
  1845. KASSERT(a_vp != b_dvp);
  1846. error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
  1847. if (error)
  1848. goto fail2;
  1849. /* Did we lose a race with mount? */
  1850. if (b_dvp->v_mountedhere != NULL) {
  1851. error = EBUSY;
  1852. goto fail3;
  1853. }
  1854. /* Make sure the caller may read the directory. */
  1855. error = VOP_ACCESS(b_dvp, VEXEC, cred, curproc);
  1856. if (error)
  1857. goto fail3;
  1858. b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
  1859. if (b_dirent != NULL) {
  1860. KASSERT(b_dirent->td_node != NULL);
  1861. /* We ruled out `.' earlier. */
  1862. KASSERT(b_dirent->td_node != b_dnode);
  1863. /* We ruled out `..' earlier. */
  1864. KASSERT(b_dirent->td_node !=
  1865. b_dnode->tn_spec.tn_dir.tn_parent);
  1866. /* b is not an ancestor of a. */
  1867. KASSERT(b_dirent->td_node != a_dnode);
  1868. /* But the source and target nodes might be the same. */
  1869. if ((a_dirent == NULL) ||
  1870. (a_dirent->td_node != b_dirent->td_node)) {
  1871. rw_enter_write(&b_dirent->td_node->tn_nlock);
  1872. error = tmpfs_vnode_get(mount, b_dirent->td_node,
  1873. &b_vp);
  1874. if (error)
  1875. goto fail3;
  1876. KASSERT(b_vp->v_mount == mount);
  1877. KASSERT(a_vp != b_vp);
  1878. /* Refuse to rename (over) a mount point. */
  1879. if ((b_vp->v_type == VDIR) &&
  1880. (b_vp->v_mountedhere != NULL)) {
  1881. error = EBUSY;
  1882. goto fail4;
  1883. }
  1884. } else {
  1885. b_vp = a_vp;
  1886. vref(b_vp);
  1887. }
  1888. } else if (!b_missing_ok) {
  1889. error = ENOENT;
  1890. goto fail3;
  1891. } else {
  1892. b_vp = NULL;
  1893. }
  1894. KASSERT(b_vp != a_dvp);
  1895. KASSERT(b_vp != b_dvp);
  1896. KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
  1897. KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
  1898. KASSERT(a_missing_ok || (a_dirent != NULL));
  1899. KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
  1900. KASSERT(b_missing_ok || (b_dirent != NULL));
  1901. KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
  1902. KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
  1903. KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
  1904. KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
  1905. KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
  1906. KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
  1907. KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
  1908. *a_dirent_ret = a_dirent;
  1909. *b_dirent_ret = b_dirent;
  1910. *a_vp_ret = a_vp;
  1911. *b_vp_ret = b_vp;
  1912. return 0;
  1913. fail4: if (b_vp != NULL) {
  1914. KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
  1915. if (b_vp != a_vp)
  1916. vput(b_vp);
  1917. else
  1918. vrele(a_vp);
  1919. }
  1920. fail3: KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
  1921. VOP_UNLOCK(b_dvp, 0, curproc);
  1922. fail2: if (a_vp != NULL) {
  1923. KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
  1924. vput(a_vp);
  1925. }
  1926. fail1: KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
  1927. VOP_UNLOCK(a_dvp, 0, curproc);
  1928. fail0: /* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
  1929. /* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
  1930. /* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
  1931. /* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
  1932. return error;
  1933. }
  1934. /*
  1935. * Shuffle the directory entries to move fvp from the directory fdvp
  1936. * into the directory tdvp. fde is fvp's directory entry in fdvp. If
  1937. * we are overwriting a target node, it is tvp, and tde is its
  1938. * directory entry in tdvp.
  1939. *
  1940. * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
  1941. */
  1942. void
  1943. tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
  1944. struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
  1945. struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
  1946. {
  1947. KASSERT(tmpfs != NULL);
  1948. KASSERT(fdvp != NULL);
  1949. KASSERT(fde != NULL);
  1950. KASSERT(fvp != NULL);
  1951. KASSERT(tdvp != NULL);
  1952. KASSERT(fde->td_node != NULL);
  1953. KASSERT(fde->td_node->tn_vnode == fvp);
  1954. KASSERT((tde == NULL) == (tvp == NULL));
  1955. KASSERT((tde == NULL) || (tde->td_node != NULL));
  1956. KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
  1957. KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
  1958. KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
  1959. KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
  1960. KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
  1961. /*
  1962. * If we are moving from one directory to another, detach the
  1963. * source entry and reattach it to the target directory.
  1964. */
  1965. if (fdvp != tdvp) {
  1966. /* tmpfs_dir_detach clobbers fde->td_node, so save it. */
  1967. struct tmpfs_node *fnode = fde->td_node;
  1968. tmpfs_node_t *fdnode = VP_TO_TMPFS_DIR(fdvp);
  1969. tmpfs_node_t *tdnode = VP_TO_TMPFS_DIR(tdvp);
  1970. tmpfs_dir_detach(fdnode, fde);
  1971. tmpfs_dir_attach(tdnode, fde, fnode);
  1972. } else if (tvp == NULL) {
  1973. /*
  1974. * We are changing the directory. tmpfs_dir_attach and
  1975. * tmpfs_dir_detach note the events for us, but for
  1976. * this case we don't call them, so we must note the
  1977. * event explicitly.
  1978. */
  1979. VN_KNOTE(fdvp, NOTE_WRITE);
  1980. }
  1981. /*
  1982. * If we are replacing an existing target entry, delete it.
  1983. */
  1984. if (tde != NULL) {
  1985. tmpfs_node_t *tdnode = VP_TO_TMPFS_DIR(tdvp);
  1986. KASSERT(tvp != NULL);
  1987. KASSERT(tde->td_node != NULL);
  1988. KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
  1989. if (tde->td_node->tn_type == VDIR) {
  1990. KASSERT(tde->td_node->tn_size == 0);
  1991. KASSERT(tde->td_node->tn_links == 2);
  1992. /* Decrement the extra link count for `.' so
  1993. * the vnode will be recycled when released. */
  1994. tde->td_node->tn_links--;
  1995. }
  1996. tmpfs_dir_detach(tdnode, tde);
  1997. tmpfs_free_dirent(tmpfs, tde);
  1998. }
  1999. }
  2000. /*
  2001. * Remove the entry de for the non-directory vp from the directory dvp.
  2002. *
  2003. * Everything must be locked and referenced.
  2004. */
  2005. int
  2006. tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
  2007. struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
  2008. struct ucred *cred)
  2009. {
  2010. int error;
  2011. KASSERT(tmpfs != NULL);
  2012. KASSERT(dvp != NULL);
  2013. KASSERT(dnode != NULL);
  2014. KASSERT(de != NULL);
  2015. KASSERT(vp != NULL);
  2016. KASSERT(dnode->tn_vnode == dvp);
  2017. KASSERT(de->td_node != NULL);
  2018. KASSERT(de->td_node->tn_vnode == vp);
  2019. KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
  2020. KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
  2021. error = tmpfs_remove_check_possible(dnode, de->td_node);
  2022. if (error)
  2023. return error;
  2024. error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
  2025. if (error)
  2026. return error;
  2027. /*
  2028. * If not root and directory is sticky, check for permission on
  2029. * directory or on file. This implements append-only directories.
  2030. */
  2031. if ((dnode->tn_mode & S_ISTXT) != 0)
  2032. if (cred->cr_uid != 0 && cred->cr_uid != dnode->tn_uid &&
  2033. cred->cr_uid != de->td_node->tn_uid)
  2034. return EPERM;
  2035. tmpfs_dir_detach(dnode, de);
  2036. tmpfs_free_dirent(tmpfs, de);
  2037. return 0;
  2038. }
  2039. /*
  2040. * Check whether a rename is possible independent of credentials.
  2041. *
  2042. * Everything must be locked and referenced.
  2043. */
  2044. int
  2045. tmpfs_rename_check_possible(
  2046. struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
  2047. struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
  2048. {
  2049. KASSERT(fdnode != NULL);
  2050. KASSERT(fnode != NULL);
  2051. KASSERT(tdnode != NULL);
  2052. KASSERT(fdnode != fnode);
  2053. KASSERT(tdnode != tnode);
  2054. KASSERT(fnode != tnode);
  2055. KASSERT(fdnode->tn_vnode != NULL);
  2056. KASSERT(fnode->tn_vnode != NULL);
  2057. KASSERT(tdnode->tn_vnode != NULL);
  2058. KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
  2059. KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
  2060. KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
  2061. KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
  2062. KASSERT((tnode == NULL) ||
  2063. (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
  2064. /*
  2065. * If fdnode is immutable, we can't write to it. If fdnode is
  2066. * append-only, the only change we can make is to add entries
  2067. * to it. If fnode is immutable, we can't change the links to
  2068. * it. If fnode is append-only...well, this is what UFS does.
  2069. */
  2070. if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
  2071. return EPERM;
  2072. /*
  2073. * If tdnode is immutable, we can't write to it. If tdnode is
  2074. * append-only, we can add entries, but we can't change
  2075. * existing entries.
  2076. */
  2077. if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
  2078. return EPERM;
  2079. /*
  2080. * If tnode is immutable, we can't replace links to it. If
  2081. * tnode is append-only...well, this is what UFS does.
  2082. */
  2083. if (tnode != NULL) {
  2084. KASSERT(tnode != NULL);
  2085. if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
  2086. return EPERM;
  2087. }
  2088. return 0;
  2089. }
  2090. /*
  2091. * Check whether a rename is permitted given our credentials.
  2092. *
  2093. * Everything must be locked and referenced.
  2094. */
  2095. int
  2096. tmpfs_rename_check_permitted(struct ucred *cred,
  2097. struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
  2098. struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
  2099. {
  2100. int error;
  2101. KASSERT(fdnode != NULL);
  2102. KASSERT(fnode != NULL);
  2103. KASSERT(tdnode != NULL);
  2104. KASSERT(fdnode != fnode);
  2105. KASSERT(tdnode != tnode);
  2106. KASSERT(fnode != tnode);
  2107. KASSERT(fdnode->tn_vnode != NULL);
  2108. KASSERT(fnode->tn_vnode != NULL);
  2109. KASSERT(tdnode->tn_vnode != NULL);
  2110. KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
  2111. KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
  2112. KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
  2113. KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
  2114. KASSERT((tnode == NULL) ||
  2115. (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
  2116. /*
  2117. * We need to remove or change an entry in the source directory.
  2118. */
  2119. error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred, curproc);
  2120. if (error)
  2121. return error;
  2122. /*
  2123. * If we are changing directories, then we need to write to the
  2124. * target directory to add or change an entry. Also, if fnode
  2125. * is a directory, we need to write to it to change its `..'
  2126. * entry.
  2127. */
  2128. if (fdnode != tdnode) {
  2129. error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred, curproc);
  2130. if (error)
  2131. return error;
  2132. if (fnode->tn_type == VDIR) {
  2133. error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred,
  2134. curproc);
  2135. if (error)
  2136. return error;
  2137. }
  2138. }
  2139. error = tmpfs_check_sticky(cred, fdnode, fnode);
  2140. if (error)
  2141. return error;
  2142. if (TMPFS_DIRSEQ_FULL(tdnode))
  2143. return (ENOSPC);
  2144. error = tmpfs_check_sticky(cred, tdnode, tnode);
  2145. if (error)
  2146. return error;
  2147. return 0;
  2148. }
  2149. /*
  2150. * Check whether removing node's entry in dnode is possible independent
  2151. * of credentials.
  2152. *
  2153. * Everything must be locked and referenced.
  2154. */
  2155. int
  2156. tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
  2157. {
  2158. KASSERT(dnode != NULL);
  2159. KASSERT(dnode->tn_vnode != NULL);
  2160. KASSERT(node != NULL);
  2161. KASSERT(dnode != node);
  2162. KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
  2163. KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
  2164. /*
  2165. * We want to delete the entry. If dnode is immutable, we
  2166. * can't write to it to delete the entry. If dnode is
  2167. * append-only, the only change we can make is to add entries,
  2168. * so we can't delete entries. If node is immutable, we can't
  2169. * change the links to it, so we can't delete the entry. If
  2170. * node is append-only...well, this is what UFS does.
  2171. */
  2172. if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
  2173. return EPERM;
  2174. return 0;
  2175. }
  2176. /*
  2177. * Check whether removing node's entry in dnode is permitted given our
  2178. * credentials.
  2179. *
  2180. * Everything must be locked and referenced.
  2181. */
  2182. int
  2183. tmpfs_remove_check_permitted(struct ucred *cred,
  2184. struct tmpfs_node *dnode, struct tmpfs_node *node)
  2185. {
  2186. int error;
  2187. KASSERT(dnode != NULL);
  2188. KASSERT(dnode->tn_vnode != NULL);
  2189. KASSERT(node != NULL);
  2190. KASSERT(dnode != node);
  2191. KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
  2192. KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
  2193. /*
  2194. * Check whether we are permitted to write to the source
  2195. * directory in order to delete an entry from it.
  2196. */
  2197. error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred, curproc);
  2198. if (error)
  2199. return error;
  2200. error = tmpfs_check_sticky(cred, dnode, node);
  2201. if (error)
  2202. return error;
  2203. return 0;
  2204. }
  2205. /*
  2206. * Check whether we may change an entry in a sticky directory. If the
  2207. * directory is sticky, the user must own either the directory or, if
  2208. * it exists, the node, in order to change the entry.
  2209. *
  2210. * Everything must be locked and referenced.
  2211. */
  2212. int
  2213. tmpfs_check_sticky(struct ucred *cred,
  2214. struct tmpfs_node *dnode, struct tmpfs_node *node)
  2215. {
  2216. KASSERT(dnode != NULL);
  2217. KASSERT(dnode->tn_vnode != NULL);
  2218. KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
  2219. KASSERT((node == NULL) || (node->tn_vnode != NULL));
  2220. KASSERT((node == NULL) ||
  2221. (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
  2222. if (node == NULL)
  2223. return 0;
  2224. if (dnode->tn_mode & S_ISTXT) {
  2225. if (cred->cr_uid != 0 &&
  2226. cred->cr_uid != dnode->tn_uid &&
  2227. cred->cr_uid != node->tn_uid)
  2228. return EPERM;
  2229. }
  2230. return 0;
  2231. }
  2232. void
  2233. tmpfs_rename_cache_purge(struct vnode *fdvp, struct vnode *fvp,
  2234. struct vnode *tdvp, struct vnode *tvp)
  2235. {
  2236. KASSERT(fdvp != NULL);
  2237. KASSERT(fvp != NULL);
  2238. KASSERT(tdvp != NULL);
  2239. KASSERT(fdvp != fvp);
  2240. KASSERT(fdvp != tvp);
  2241. KASSERT(tdvp != fvp);
  2242. KASSERT(tdvp != tvp);
  2243. KASSERT(fvp != tvp);
  2244. KASSERT(fdvp->v_type == VDIR);
  2245. KASSERT(tdvp->v_type == VDIR);
  2246. /*
  2247. * XXX What actually needs to be purged?
  2248. */
  2249. cache_purge(fdvp);
  2250. if (fvp->v_type == VDIR)
  2251. cache_purge(fvp);
  2252. if (tdvp != fdvp)
  2253. cache_purge(tdvp);
  2254. if ((tvp != NULL) && (tvp->v_type == VDIR))
  2255. cache_purge(tvp);
  2256. }
  2257. void
  2258. tmpfs_rename_abort(void *v)
  2259. {
  2260. struct vop_rename_args *ap = v;
  2261. struct vnode *fdvp = ap->a_fdvp;
  2262. struct vnode *fvp = ap->a_fvp;
  2263. struct componentname *fcnp = ap->a_fcnp;
  2264. struct vnode *tdvp = ap->a_tdvp;
  2265. struct vnode *tvp = ap->a_tvp;
  2266. struct componentname *tcnp = ap->a_tcnp;
  2267. VOP_ABORTOP(tdvp, tcnp);
  2268. if (tdvp == tvp)
  2269. vrele(tdvp);
  2270. else
  2271. vput(tdvp);
  2272. if (tvp != NULL)
  2273. vput(tvp);
  2274. VOP_ABORTOP(fdvp, fcnp);
  2275. vrele(fdvp);
  2276. vrele(fvp);
  2277. }
  2278. void filt_tmpfsdetach(struct knote *kn);
  2279. int filt_tmpfsread(struct knote *kn, long hint);
  2280. int filt_tmpfswrite(struct knote *kn, long hint);
  2281. int filt_tmpfsvnode(struct knote *kn, long hint);
  2282. struct filterops tmpfsread_filtops =
  2283. { 1, NULL, filt_tmpfsdetach, filt_tmpfsread };
  2284. struct filterops tmpfswrite_filtops =
  2285. { 1, NULL, filt_tmpfsdetach, filt_tmpfswrite };
  2286. struct filterops tmpfsvnode_filtops =
  2287. { 1, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
  2288. int
  2289. tmpfs_kqfilter(void *v)
  2290. {
  2291. struct vop_kqfilter_args *ap = v;
  2292. struct vnode *vp = ap->a_vp;
  2293. struct knote *kn = ap->a_kn;
  2294. switch (kn->kn_filter) {
  2295. case EVFILT_READ:
  2296. kn->kn_fop = &tmpfsread_filtops;
  2297. break;
  2298. case EVFILT_WRITE:
  2299. kn->kn_fop = &tmpfswrite_filtops;
  2300. break;
  2301. case EVFILT_VNODE:
  2302. kn->kn_fop = &tmpfsvnode_filtops;
  2303. break;
  2304. default:
  2305. return (EINVAL);
  2306. }
  2307. kn->kn_hook = (caddr_t)vp;
  2308. SLIST_INSERT_HEAD(&vp->v_selectinfo.si_note, kn, kn_selnext);
  2309. return (0);
  2310. }
  2311. void
  2312. filt_tmpfsdetach(struct knote *kn)
  2313. {
  2314. struct vnode *vp = (struct vnode *)kn->kn_hook;
  2315. SLIST_REMOVE(&vp->v_selectinfo.si_note, kn, knote, kn_selnext);
  2316. }
  2317. int
  2318. filt_tmpfsread(struct knote *kn, long hint)
  2319. {
  2320. struct vnode *vp = (struct vnode *)kn->kn_hook;
  2321. tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
  2322. /*
  2323. * filesystem is gone, so set the EOF flag and schedule
  2324. * the knote for deletion.
  2325. */
  2326. if (hint == NOTE_REVOKE) {
  2327. kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  2328. return (1);
  2329. }
  2330. kn->kn_data = node->tn_size - kn->kn_fp->f_offset;
  2331. if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
  2332. kn->kn_fflags |= NOTE_EOF;
  2333. return (1);
  2334. }
  2335. return (kn->kn_data != 0);
  2336. }
  2337. int
  2338. filt_tmpfswrite(struct knote *kn, long hint)
  2339. {
  2340. /*
  2341. * filesystem is gone, so set the EOF flag and schedule
  2342. * the knote for deletion.
  2343. */
  2344. if (hint == NOTE_REVOKE) {
  2345. kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  2346. return (1);
  2347. }
  2348. kn->kn_data = 0;
  2349. return (1);
  2350. }
  2351. int
  2352. filt_tmpfsvnode(struct knote *kn, long hint)
  2353. {
  2354. if (kn->kn_sfflags & hint)
  2355. kn->kn_fflags |= hint;
  2356. if (hint == NOTE_REVOKE) {
  2357. kn->kn_flags |= EV_EOF;
  2358. return (1);
  2359. }
  2360. return (kn->kn_fflags != 0);
  2361. }