msdosfs_vnops.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. /* $OpenBSD: msdosfs_vnops.c,v 1.100 2015/04/17 04:43:21 guenther Exp $ */
  2. /* $NetBSD: msdosfs_vnops.c,v 1.63 1997/10/17 11:24:19 ws Exp $ */
  3. /*-
  4. * Copyright (C) 2005 Thomas Wang.
  5. * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
  6. * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
  7. * All rights reserved.
  8. * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * This product includes software developed by TooLs GmbH.
  21. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  22. * derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  25. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  30. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  33. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Written by Paul Popelka (paulp@uts.amdahl.com)
  37. *
  38. * You can do anything you want with this software, just don't say you wrote
  39. * it, and don't remove this notice.
  40. *
  41. * This software is provided "as is".
  42. *
  43. * The author supplies this software to be publicly redistributed on the
  44. * understanding that the author is not responsible for the correct
  45. * functioning of this software in any circumstances and is not liable for
  46. * any damages caused by this software.
  47. *
  48. * October 1992
  49. */
  50. #include <sys/param.h>
  51. #include <sys/systm.h>
  52. #include <sys/namei.h>
  53. #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
  54. #include <sys/file.h> /* define FWRITE ... */
  55. #include <sys/stat.h>
  56. #include <sys/buf.h>
  57. #include <sys/proc.h>
  58. #include <sys/mount.h>
  59. #include <sys/vnode.h>
  60. #include <sys/lock.h>
  61. #include <sys/signalvar.h>
  62. #include <sys/specdev.h> /* XXX */ /* defines v_rdev */
  63. #include <sys/malloc.h>
  64. #include <sys/pool.h>
  65. #include <sys/dirent.h> /* defines dirent structure */
  66. #include <sys/lockf.h>
  67. #include <sys/poll.h>
  68. #include <sys/unistd.h>
  69. #include <msdosfs/bpb.h>
  70. #include <msdosfs/direntry.h>
  71. #include <msdosfs/denode.h>
  72. #include <msdosfs/msdosfsmount.h>
  73. #include <msdosfs/fat.h>
  74. static uint32_t fileidhash(uint64_t);
  75. int msdosfs_kqfilter(void *);
  76. int filt_msdosfsread(struct knote *, long);
  77. int filt_msdosfswrite(struct knote *, long);
  78. int filt_msdosfsvnode(struct knote *, long);
  79. void filt_msdosfsdetach(struct knote *);
  80. /*
  81. * Some general notes:
  82. *
  83. * In the ufs filesystem the inodes, superblocks, and indirect blocks are
  84. * read/written using the vnode for the filesystem. Blocks that represent
  85. * the contents of a file are read/written using the vnode for the file
  86. * (including directories when they are read/written as files). This
  87. * presents problems for the dos filesystem because data that should be in
  88. * an inode (if dos had them) resides in the directory itself. Since we
  89. * must update directory entries without the benefit of having the vnode
  90. * for the directory we must use the vnode for the filesystem. This means
  91. * that when a directory is actually read/written (via read, write, or
  92. * readdir, or seek) we must use the vnode for the filesystem instead of
  93. * the vnode for the directory as would happen in ufs. This is to insure we
  94. * retrieve the correct block from the buffer cache since the hash value is
  95. * based upon the vnode address and the desired block number.
  96. */
  97. /*
  98. * Create a regular file. On entry the directory to contain the file being
  99. * created is locked. We must release before we return. We must also free
  100. * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
  101. * only if the SAVESTART bit in cn_flags is clear on success.
  102. */
  103. int
  104. msdosfs_create(void *v)
  105. {
  106. struct vop_create_args *ap = v;
  107. struct componentname *cnp = ap->a_cnp;
  108. struct denode ndirent;
  109. struct denode *dep;
  110. struct denode *pdep = VTODE(ap->a_dvp);
  111. int error;
  112. struct timespec ts;
  113. #ifdef MSDOSFS_DEBUG
  114. printf("msdosfs_create(cnp %08x, vap %08x\n", cnp, ap->a_vap);
  115. #endif
  116. /*
  117. * If this is the root directory and there is no space left we
  118. * can't do anything. This is because the root directory can not
  119. * change size.
  120. */
  121. if (pdep->de_StartCluster == MSDOSFSROOT
  122. && pdep->de_fndoffset >= pdep->de_FileSize) {
  123. error = ENOSPC;
  124. goto bad;
  125. }
  126. /*
  127. * Create a directory entry for the file, then call createde() to
  128. * have it installed. NOTE: DOS files are always executable. We
  129. * use the absence of the owner write bit to make the file
  130. * readonly.
  131. */
  132. #ifdef DIAGNOSTIC
  133. if ((cnp->cn_flags & HASBUF) == 0)
  134. panic("msdosfs_create: no name");
  135. #endif
  136. bzero(&ndirent, sizeof(ndirent));
  137. if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
  138. goto bad;
  139. ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
  140. ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
  141. ndirent.de_StartCluster = 0;
  142. ndirent.de_FileSize = 0;
  143. ndirent.de_dev = pdep->de_dev;
  144. ndirent.de_devvp = pdep->de_devvp;
  145. ndirent.de_pmp = pdep->de_pmp;
  146. ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
  147. getnanotime(&ts);
  148. DETIMES(&ndirent, &ts, &ts, &ts);
  149. if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
  150. goto bad;
  151. if ((cnp->cn_flags & SAVESTART) == 0)
  152. pool_put(&namei_pool, cnp->cn_pnbuf);
  153. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  154. vput(ap->a_dvp);
  155. *ap->a_vpp = DETOV(dep);
  156. return (0);
  157. bad:
  158. pool_put(&namei_pool, cnp->cn_pnbuf);
  159. vput(ap->a_dvp);
  160. return (error);
  161. }
  162. int
  163. msdosfs_mknod(void *v)
  164. {
  165. struct vop_mknod_args *ap = v;
  166. pool_put(&namei_pool, ap->a_cnp->cn_pnbuf);
  167. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  168. vput(ap->a_dvp);
  169. return (EINVAL);
  170. }
  171. int
  172. msdosfs_open(void *v)
  173. {
  174. #if 0
  175. struct vop_open_args /* {
  176. struct vnode *a_vp;
  177. int a_mode;
  178. struct ucred *a_cred;
  179. struct proc *a_p;
  180. } */ *ap;
  181. #endif
  182. return (0);
  183. }
  184. int
  185. msdosfs_close(void *v)
  186. {
  187. struct vop_close_args *ap = v;
  188. struct vnode *vp = ap->a_vp;
  189. struct denode *dep = VTODE(vp);
  190. struct timespec ts;
  191. if (vp->v_usecount > 1 && !VOP_ISLOCKED(vp)) {
  192. getnanotime(&ts);
  193. DETIMES(dep, &ts, &ts, &ts);
  194. }
  195. return (0);
  196. }
  197. int
  198. msdosfs_access(void *v)
  199. {
  200. struct vop_access_args *ap = v;
  201. struct denode *dep = VTODE(ap->a_vp);
  202. struct msdosfsmount *pmp = dep->de_pmp;
  203. mode_t dosmode;
  204. dosmode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH);
  205. if ((dep->de_Attributes & ATTR_READONLY) == 0)
  206. dosmode |= (S_IWUSR|S_IWGRP|S_IWOTH);
  207. dosmode &= pmp->pm_mask;
  208. if (dep->de_Attributes & ATTR_DIRECTORY
  209. && pmp->pm_flags & MSDOSFSMNT_ALLOWDIRX) {
  210. dosmode |= (dosmode & S_IRUSR) ? S_IXUSR : 0;
  211. dosmode |= (dosmode & S_IRGRP) ? S_IXGRP : 0;
  212. dosmode |= (dosmode & S_IROTH) ? S_IXOTH : 0;
  213. }
  214. return (vaccess(ap->a_vp->v_type, dosmode, pmp->pm_uid, pmp->pm_gid,
  215. ap->a_mode, ap->a_cred));
  216. }
  217. int
  218. msdosfs_getattr(void *v)
  219. {
  220. struct vop_getattr_args *ap = v;
  221. struct denode *dep = VTODE(ap->a_vp);
  222. struct msdosfsmount *pmp = dep->de_pmp;
  223. struct vattr *vap = ap->a_vap;
  224. struct timespec ts;
  225. uint32_t fileid;
  226. getnanotime(&ts);
  227. DETIMES(dep, &ts, &ts, &ts);
  228. vap->va_fsid = dep->de_dev;
  229. /*
  230. * The following computation of the fileid must be the same as
  231. * that used in msdosfs_readdir() to compute d_fileno. If not,
  232. * pwd doesn't work.
  233. *
  234. * We now use the starting cluster number as the fileid/fileno.
  235. * This works for both files and directories (including the root
  236. * directory, on FAT32). Even on FAT32, this will at most be a
  237. * 28-bit number, as the high 4 bits of FAT32 cluster numbers
  238. * are reserved.
  239. *
  240. * However, we do need to do something for 0-length files, which
  241. * will not have a starting cluster number.
  242. *
  243. * These files cannot be directories, since (except for /, which
  244. * is special-cased anyway) directories contain entries for . and
  245. * .., so must have non-zero length.
  246. *
  247. * In this case, we just create a non-cryptographic hash of the
  248. * original fileid calculation, and set the top bit.
  249. *
  250. * This algorithm has the benefit that all directories, and all
  251. * non-zero-length files, will have fileids that are persistent
  252. * across mounts and reboots, and that cannot collide (as long
  253. * as the filesystem is not corrupt). Zero-length files will
  254. * have fileids that are persistent, but that may collide. We
  255. * will just have to live with that.
  256. */
  257. fileid = dep->de_StartCluster;
  258. if (dep->de_Attributes & ATTR_DIRECTORY) {
  259. /* Special-case root */
  260. if (dep->de_StartCluster == MSDOSFSROOT)
  261. fileid = FAT32(pmp) ? pmp->pm_rootdirblk : 1;
  262. } else {
  263. if (dep->de_FileSize == 0) {
  264. uint32_t dirsperblk;
  265. uint64_t fileid64;
  266. dirsperblk = pmp->pm_BytesPerSec /
  267. sizeof(struct direntry);
  268. fileid64 = (dep->de_dirclust == MSDOSFSROOT) ?
  269. roottobn(pmp, 0) : cntobn(pmp, dep->de_dirclust);
  270. fileid64 *= dirsperblk;
  271. fileid64 += dep->de_diroffset / sizeof(struct direntry);
  272. fileid = fileidhash(fileid64);
  273. }
  274. }
  275. vap->va_fileid = fileid;
  276. vap->va_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
  277. ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
  278. vap->va_mode &= dep->de_pmp->pm_mask;
  279. if (dep->de_Attributes & ATTR_DIRECTORY) {
  280. vap->va_mode |= S_IFDIR;
  281. if (pmp->pm_flags & MSDOSFSMNT_ALLOWDIRX) {
  282. vap->va_mode |= (vap->va_mode & S_IRUSR) ? S_IXUSR : 0;
  283. vap->va_mode |= (vap->va_mode & S_IRGRP) ? S_IXGRP : 0;
  284. vap->va_mode |= (vap->va_mode & S_IROTH) ? S_IXOTH : 0;
  285. }
  286. }
  287. vap->va_nlink = 1;
  288. vap->va_gid = dep->de_pmp->pm_gid;
  289. vap->va_uid = dep->de_pmp->pm_uid;
  290. vap->va_rdev = 0;
  291. vap->va_size = dep->de_FileSize;
  292. dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
  293. if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
  294. dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
  295. dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CTimeHundredth, &vap->va_ctime);
  296. } else {
  297. vap->va_atime = vap->va_mtime;
  298. vap->va_ctime = vap->va_mtime;
  299. }
  300. vap->va_flags = 0;
  301. if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
  302. vap->va_flags |= SF_ARCHIVED;
  303. vap->va_gen = 0;
  304. vap->va_blocksize = dep->de_pmp->pm_bpcluster;
  305. vap->va_bytes = (dep->de_FileSize + dep->de_pmp->pm_crbomask) &
  306. ~(dep->de_pmp->pm_crbomask);
  307. vap->va_type = ap->a_vp->v_type;
  308. return (0);
  309. }
  310. int
  311. msdosfs_setattr(void *v)
  312. {
  313. struct vop_setattr_args *ap = v;
  314. struct vnode *vp = ap->a_vp;
  315. struct denode *dep = VTODE(ap->a_vp);
  316. struct msdosfsmount *pmp = dep->de_pmp;
  317. struct vattr *vap = ap->a_vap;
  318. struct ucred *cred = ap->a_cred;
  319. int error = 0;
  320. #ifdef MSDOSFS_DEBUG
  321. printf("msdosfs_setattr(): vp %08x, vap %08x, cred %08x, p %08x\n",
  322. ap->a_vp, vap, cred, ap->a_p);
  323. #endif
  324. if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
  325. (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
  326. (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
  327. (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
  328. #ifdef MSDOSFS_DEBUG
  329. printf("msdosfs_setattr(): returning EINVAL\n");
  330. printf(" va_type %d, va_nlink %x, va_fsid %x, va_fileid %x\n",
  331. vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
  332. printf(" va_blocksize %x, va_rdev %x, va_bytes %x, va_gen %x\n",
  333. vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
  334. #endif
  335. return (EINVAL);
  336. }
  337. if (vap->va_flags != VNOVAL) {
  338. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  339. return (EINVAL);
  340. if (cred->cr_uid != pmp->pm_uid) {
  341. error = suser_ucred(cred);
  342. if (error)
  343. return (error);
  344. }
  345. /*
  346. * We are very inconsistent about handling unsupported
  347. * attributes. We ignored the access time and the
  348. * read and execute bits. We were strict for the other
  349. * attributes.
  350. *
  351. * Here we are strict, stricter than ufs in not allowing
  352. * users to attempt to set SF_SETTABLE bits or anyone to
  353. * set unsupported bits. However, we ignore attempts to
  354. * set ATTR_ARCHIVE for directories `cp -pr' from a more
  355. * sensible filesystem attempts it a lot.
  356. */
  357. if (vap->va_flags & SF_SETTABLE) {
  358. error = suser_ucred(cred);
  359. if (error)
  360. return (error);
  361. }
  362. if (vap->va_flags & ~SF_ARCHIVED)
  363. return EOPNOTSUPP;
  364. if (vap->va_flags & SF_ARCHIVED)
  365. dep->de_Attributes &= ~ATTR_ARCHIVE;
  366. else if (!(dep->de_Attributes & ATTR_DIRECTORY))
  367. dep->de_Attributes |= ATTR_ARCHIVE;
  368. dep->de_flag |= DE_MODIFIED;
  369. }
  370. if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
  371. uid_t uid;
  372. gid_t gid;
  373. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  374. return (EINVAL);
  375. uid = vap->va_uid;
  376. if (uid == (uid_t)VNOVAL)
  377. uid = pmp->pm_uid;
  378. gid = vap->va_gid;
  379. if (gid == (gid_t)VNOVAL)
  380. gid = pmp->pm_gid;
  381. if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
  382. (gid != pmp->pm_gid && !groupmember(gid, cred))) {
  383. error = suser_ucred(cred);
  384. if (error)
  385. return (error);
  386. }
  387. if (uid != pmp->pm_uid || gid != pmp->pm_gid)
  388. return EINVAL;
  389. }
  390. if (vap->va_size != VNOVAL) {
  391. switch (vp->v_type) {
  392. case VDIR:
  393. return (EISDIR);
  394. case VREG:
  395. /*
  396. * Truncation is only supported for regular files,
  397. * Disallow it if the filesystem is read-only.
  398. */
  399. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  400. return (EINVAL);
  401. break;
  402. default:
  403. /*
  404. * According to POSIX, the result is unspecified
  405. * for file types other than regular files,
  406. * directories and shared memory objects. We
  407. * don't support any file types except regular
  408. * files and directories in this file system, so
  409. * this (default) case is unreachable and can do
  410. * anything. Keep falling through to detrunc()
  411. * for now.
  412. */
  413. break;
  414. }
  415. error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
  416. if (error)
  417. return error;
  418. }
  419. if ((vap->va_vaflags & VA_UTIMES_CHANGE) ||
  420. vap->va_atime.tv_nsec != VNOVAL ||
  421. vap->va_mtime.tv_nsec != VNOVAL) {
  422. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  423. return (EINVAL);
  424. if (cred->cr_uid != pmp->pm_uid &&
  425. (error = suser_ucred(cred)) &&
  426. ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
  427. (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_p))))
  428. return (error);
  429. if (vp->v_type != VDIR) {
  430. if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
  431. vap->va_atime.tv_nsec != VNOVAL) {
  432. dep->de_flag &= ~DE_ACCESS;
  433. unix2dostime(&vap->va_atime, &dep->de_ADate,
  434. NULL, NULL);
  435. }
  436. if (vap->va_mtime.tv_nsec != VNOVAL) {
  437. dep->de_flag &= ~DE_UPDATE;
  438. unix2dostime(&vap->va_mtime, &dep->de_MDate,
  439. &dep->de_MTime, NULL);
  440. }
  441. dep->de_Attributes |= ATTR_ARCHIVE;
  442. dep->de_flag |= DE_MODIFIED;
  443. }
  444. }
  445. /*
  446. * DOS files only have the ability to have their writability
  447. * attribute set, so we use the owner write bit to set the readonly
  448. * attribute.
  449. */
  450. if (vap->va_mode != (mode_t)VNOVAL) {
  451. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  452. return (EINVAL);
  453. if (cred->cr_uid != pmp->pm_uid) {
  454. error = suser_ucred(cred);
  455. if (error)
  456. return (error);
  457. }
  458. if (vp->v_type != VDIR) {
  459. /* We ignore the read and execute bits. */
  460. if (vap->va_mode & VWRITE)
  461. dep->de_Attributes &= ~ATTR_READONLY;
  462. else
  463. dep->de_Attributes |= ATTR_READONLY;
  464. dep->de_Attributes |= ATTR_ARCHIVE;
  465. dep->de_flag |= DE_MODIFIED;
  466. }
  467. }
  468. VN_KNOTE(ap->a_vp, NOTE_ATTRIB);
  469. return (deupdat(dep, 1));
  470. }
  471. int
  472. msdosfs_read(void *v)
  473. {
  474. struct vop_read_args *ap = v;
  475. int error = 0;
  476. uint32_t diff;
  477. int blsize;
  478. int isadir;
  479. uint32_t n;
  480. long on;
  481. daddr_t lbn, rablock, rablkno;
  482. struct buf *bp;
  483. struct vnode *vp = ap->a_vp;
  484. struct denode *dep = VTODE(vp);
  485. struct msdosfsmount *pmp = dep->de_pmp;
  486. struct uio *uio = ap->a_uio;
  487. /*
  488. * If they didn't ask for any data, then we are done.
  489. */
  490. if (uio->uio_resid == 0)
  491. return (0);
  492. if (uio->uio_offset < 0)
  493. return (EINVAL);
  494. isadir = dep->de_Attributes & ATTR_DIRECTORY;
  495. do {
  496. if (uio->uio_offset >= dep->de_FileSize)
  497. return (0);
  498. lbn = de_cluster(pmp, uio->uio_offset);
  499. on = uio->uio_offset & pmp->pm_crbomask;
  500. n = min((uint32_t) (pmp->pm_bpcluster - on), uio->uio_resid);
  501. /*
  502. * de_FileSize is uint32_t, and we know that uio_offset <
  503. * de_FileSize, so uio->uio_offset < 2^32. Therefore
  504. * the cast to uint32_t on the next line is safe.
  505. */
  506. diff = dep->de_FileSize - (uint32_t)uio->uio_offset;
  507. if (diff < n)
  508. n = diff;
  509. /* convert cluster # to block # if a directory */
  510. if (isadir) {
  511. error = pcbmap(dep, lbn, &lbn, 0, &blsize);
  512. if (error)
  513. return (error);
  514. }
  515. /*
  516. * If we are operating on a directory file then be sure to
  517. * do i/o with the vnode for the filesystem instead of the
  518. * vnode for the directory.
  519. */
  520. if (isadir) {
  521. error = bread(pmp->pm_devvp, lbn, blsize, &bp);
  522. } else {
  523. rablock = lbn + 1;
  524. rablkno = de_cn2bn(pmp, rablock);
  525. if (dep->de_lastr + 1 == lbn &&
  526. de_cn2off(pmp, rablock) < dep->de_FileSize)
  527. error = breadn(vp, de_cn2bn(pmp, lbn),
  528. pmp->pm_bpcluster, &rablkno,
  529. &pmp->pm_bpcluster, 1, &bp);
  530. else
  531. error = bread(vp, de_cn2bn(pmp, lbn),
  532. pmp->pm_bpcluster, &bp);
  533. dep->de_lastr = lbn;
  534. }
  535. n = min(n, pmp->pm_bpcluster - bp->b_resid);
  536. if (error) {
  537. brelse(bp);
  538. return (error);
  539. }
  540. error = uiomovei(bp->b_data + on, (int) n, uio);
  541. brelse(bp);
  542. } while (error == 0 && uio->uio_resid > 0 && n != 0);
  543. if (!isadir && !(vp->v_mount->mnt_flag & MNT_NOATIME))
  544. dep->de_flag |= DE_ACCESS;
  545. return (error);
  546. }
  547. /*
  548. * Write data to a file or directory.
  549. */
  550. int
  551. msdosfs_write(void *v)
  552. {
  553. struct vop_write_args *ap = v;
  554. int n;
  555. int croffset;
  556. int resid;
  557. ssize_t overrun;
  558. int extended = 0;
  559. uint32_t osize;
  560. int error = 0;
  561. uint32_t count, lastcn;
  562. daddr_t bn;
  563. struct buf *bp;
  564. int ioflag = ap->a_ioflag;
  565. struct uio *uio = ap->a_uio;
  566. struct vnode *vp = ap->a_vp;
  567. struct vnode *thisvp;
  568. struct denode *dep = VTODE(vp);
  569. struct msdosfsmount *pmp = dep->de_pmp;
  570. struct ucred *cred = ap->a_cred;
  571. #ifdef MSDOSFS_DEBUG
  572. printf("msdosfs_write(vp %08x, uio %08x, ioflag %08x, cred %08x\n",
  573. vp, uio, ioflag, cred);
  574. printf("msdosfs_write(): diroff %d, dirclust %d, startcluster %d\n",
  575. dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
  576. #endif
  577. switch (vp->v_type) {
  578. case VREG:
  579. if (ioflag & IO_APPEND)
  580. uio->uio_offset = dep->de_FileSize;
  581. thisvp = vp;
  582. break;
  583. case VDIR:
  584. return EISDIR;
  585. default:
  586. panic("msdosfs_write(): bad file type");
  587. }
  588. if (uio->uio_offset < 0)
  589. return (EINVAL);
  590. if (uio->uio_resid == 0)
  591. return (0);
  592. /* Don't bother to try to write files larger than the f/s limit */
  593. if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
  594. return (EFBIG);
  595. /* do the filesize rlimit check */
  596. if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
  597. return (error);
  598. /*
  599. * If the offset we are starting the write at is beyond the end of
  600. * the file, then they've done a seek. Unix filesystems allow
  601. * files with holes in them, DOS doesn't so we must fill the hole
  602. * with zeroed blocks.
  603. */
  604. if (uio->uio_offset > dep->de_FileSize) {
  605. if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
  606. goto out;
  607. }
  608. /*
  609. * Remember some values in case the write fails.
  610. */
  611. resid = uio->uio_resid;
  612. osize = dep->de_FileSize;
  613. /*
  614. * If we write beyond the end of the file, extend it to its ultimate
  615. * size ahead of the time to hopefully get a contiguous area.
  616. */
  617. if (uio->uio_offset + resid > osize) {
  618. extended = 1;
  619. count = de_clcount(pmp, uio->uio_offset + resid) -
  620. de_clcount(pmp, osize);
  621. if ((error = extendfile(dep, count, NULL, NULL, 0)) &&
  622. (error != ENOSPC || (ioflag & IO_UNIT)))
  623. goto errexit;
  624. lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
  625. } else
  626. lastcn = de_clcount(pmp, osize) - 1;
  627. do {
  628. if (de_cluster(pmp, uio->uio_offset) > lastcn) {
  629. error = ENOSPC;
  630. break;
  631. }
  632. bn = de_blk(pmp, uio->uio_offset);
  633. if ((uio->uio_offset & pmp->pm_crbomask) == 0
  634. && (de_blk(pmp, uio->uio_offset + uio->uio_resid) > de_blk(pmp, uio->uio_offset)
  635. || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
  636. /*
  637. * If either the whole cluster gets written,
  638. * or we write the cluster from its start beyond EOF,
  639. * then no need to read data from disk.
  640. */
  641. bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
  642. clrbuf(bp);
  643. /*
  644. * Do the bmap now, since pcbmap needs buffers
  645. * for the fat table. (see msdosfs_strategy)
  646. */
  647. if (bp->b_blkno == bp->b_lblkno) {
  648. error = pcbmap(dep,
  649. de_bn2cn(pmp, bp->b_lblkno),
  650. &bp->b_blkno, 0, 0);
  651. if (error)
  652. bp->b_blkno = -1;
  653. }
  654. if (bp->b_blkno == -1) {
  655. brelse(bp);
  656. if (!error)
  657. error = EIO; /* XXX */
  658. break;
  659. }
  660. } else {
  661. /*
  662. * The block we need to write into exists, so read it in.
  663. */
  664. error = bread(thisvp, bn, pmp->pm_bpcluster, &bp);
  665. if (error) {
  666. brelse(bp);
  667. break;
  668. }
  669. }
  670. croffset = uio->uio_offset & pmp->pm_crbomask;
  671. n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
  672. if (uio->uio_offset + n > dep->de_FileSize) {
  673. dep->de_FileSize = uio->uio_offset + n;
  674. uvm_vnp_setsize(vp, dep->de_FileSize);
  675. }
  676. uvm_vnp_uncache(vp);
  677. /*
  678. * Should these vnode_pager_* functions be done on dir
  679. * files?
  680. */
  681. /*
  682. * Copy the data from user space into the buf header.
  683. */
  684. error = uiomovei(bp->b_data + croffset, n, uio);
  685. /*
  686. * If they want this synchronous then write it and wait for
  687. * it. Otherwise, if on a cluster boundary write it
  688. * asynchronously so we can move on to the next block
  689. * without delay. Otherwise do a delayed write because we
  690. * may want to write somemore into the block later.
  691. */
  692. #if 0
  693. if (ioflag & IO_NOCACHE)
  694. bp->b_flags |= B_NOCACHE;
  695. #endif
  696. if (ioflag & IO_SYNC)
  697. (void) bwrite(bp);
  698. else if (n + croffset == pmp->pm_bpcluster)
  699. bawrite(bp);
  700. else
  701. bdwrite(bp);
  702. dep->de_flag |= DE_UPDATE;
  703. } while (error == 0 && uio->uio_resid > 0);
  704. if (resid > uio->uio_resid)
  705. VN_KNOTE(ap->a_vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
  706. if (dep->de_FileSize < osize)
  707. VN_KNOTE(ap->a_vp, NOTE_TRUNCATE);
  708. /*
  709. * If the write failed and they want us to, truncate the file back
  710. * to the size it was before the write was attempted.
  711. */
  712. errexit:
  713. if (error) {
  714. if (ioflag & IO_UNIT) {
  715. detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
  716. uio->uio_offset -= resid - uio->uio_resid;
  717. uio->uio_resid = resid;
  718. } else {
  719. detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
  720. if (uio->uio_resid != resid)
  721. error = 0;
  722. }
  723. } else if (ioflag & IO_SYNC)
  724. error = deupdat(dep, 1);
  725. out:
  726. /* correct the result for writes clamped by vn_fsizechk() */
  727. uio->uio_resid += overrun;
  728. return (error);
  729. }
  730. int
  731. msdosfs_ioctl(void *v)
  732. {
  733. #if 0
  734. struct vop_ioctl_args /* {
  735. struct vnode *a_vp;
  736. uint32_t a_command;
  737. caddr_t a_data;
  738. int a_fflag;
  739. struct ucred *a_cred;
  740. struct proc *a_p;
  741. } */ *ap;
  742. #endif
  743. return (ENOTTY);
  744. }
  745. int
  746. msdosfs_poll(void *v)
  747. {
  748. struct vop_poll_args *ap = v;
  749. return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
  750. }
  751. /*
  752. * Flush the blocks of a file to disk.
  753. *
  754. * This function is worthless for vnodes that represent directories. Maybe we
  755. * could just do a sync if they try an fsync on a directory file.
  756. */
  757. int
  758. msdosfs_fsync(void *v)
  759. {
  760. struct vop_fsync_args *ap = v;
  761. struct vnode *vp = ap->a_vp;
  762. vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
  763. return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
  764. }
  765. /*
  766. * Flush the blocks of a file to disk.
  767. *
  768. * This function is worthless for vnodes that represent directories. Maybe we
  769. * could just do a sync if they try an fsync on a directory file.
  770. */
  771. int
  772. msdosfs_remove(void *v)
  773. {
  774. struct vop_remove_args *ap = v;
  775. struct denode *dep = VTODE(ap->a_vp);
  776. struct denode *ddep = VTODE(ap->a_dvp);
  777. int error;
  778. if (ap->a_vp->v_type == VDIR)
  779. error = EPERM;
  780. else
  781. error = removede(ddep, dep);
  782. VN_KNOTE(ap->a_vp, NOTE_DELETE);
  783. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  784. #ifdef MSDOSFS_DEBUG
  785. printf("msdosfs_remove(), dep %08x, v_usecount %d\n", dep, ap->a_vp->v_usecount);
  786. #endif
  787. if (ddep == dep)
  788. vrele(ap->a_vp);
  789. else
  790. vput(ap->a_vp); /* causes msdosfs_inactive() to be called
  791. * via vrele() */
  792. vput(ap->a_dvp);
  793. return (error);
  794. }
  795. /*
  796. * DOS filesystems don't know what links are. But since we already called
  797. * msdosfs_lookup() with create and lockparent, the parent is locked so we
  798. * have to free it before we return the error.
  799. */
  800. int
  801. msdosfs_link(void *v)
  802. {
  803. struct vop_link_args *ap = v;
  804. VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
  805. vput(ap->a_dvp);
  806. return (EOPNOTSUPP);
  807. }
  808. /*
  809. * Renames on files require moving the denode to a new hash queue since the
  810. * denode's location is used to compute which hash queue to put the file
  811. * in. Unless it is a rename in place. For example "mv a b".
  812. *
  813. * What follows is the basic algorithm:
  814. *
  815. * if (file move) {
  816. * if (dest file exists) {
  817. * remove dest file
  818. * }
  819. * if (dest and src in same directory) {
  820. * rewrite name in existing directory slot
  821. * } else {
  822. * write new entry in dest directory
  823. * update offset and dirclust in denode
  824. * move denode to new hash chain
  825. * clear old directory entry
  826. * }
  827. * } else {
  828. * directory move
  829. * if (dest directory exists) {
  830. * if (dest is not empty) {
  831. * return ENOTEMPTY
  832. * }
  833. * remove dest directory
  834. * }
  835. * if (dest and src in same directory) {
  836. * rewrite name in existing entry
  837. * } else {
  838. * be sure dest is not a child of src directory
  839. * write entry in dest directory
  840. * update "." and ".." in moved directory
  841. * update offset and dirclust in denode
  842. * move denode to new hash chain
  843. * clear old directory entry for moved directory
  844. * }
  845. * }
  846. *
  847. * On entry:
  848. * source's parent directory is unlocked
  849. * source file or directory is unlocked
  850. * destination's parent directory is locked
  851. * destination file or directory is locked if it exists
  852. *
  853. * On exit:
  854. * all denodes should be released
  855. *
  856. * Notes:
  857. * I'm not sure how the memory containing the pathnames pointed at by the
  858. * componentname structures is freed, there may be some memory bleeding
  859. * for each rename done.
  860. */
  861. int
  862. msdosfs_rename(void *v)
  863. {
  864. struct vop_rename_args *ap = v;
  865. struct vnode *tvp = ap->a_tvp;
  866. struct vnode *tdvp = ap->a_tdvp;
  867. struct vnode *fvp = ap->a_fvp;
  868. struct vnode *fdvp = ap->a_fdvp;
  869. struct componentname *tcnp = ap->a_tcnp;
  870. struct componentname *fcnp = ap->a_fcnp;
  871. struct proc *p = curproc; /* XXX */
  872. struct denode *ip, *xp, *dp, *zp;
  873. u_char toname[11], oldname[11];
  874. uint32_t from_diroffset, to_diroffset;
  875. u_char to_count;
  876. int doingdirectory = 0, newparent = 0;
  877. int error;
  878. uint32_t cn, pcl;
  879. daddr_t bn;
  880. struct msdosfsmount *pmp;
  881. struct direntry *dotdotp;
  882. struct buf *bp;
  883. pmp = VFSTOMSDOSFS(fdvp->v_mount);
  884. #ifdef DIAGNOSTIC
  885. if ((tcnp->cn_flags & HASBUF) == 0 ||
  886. (fcnp->cn_flags & HASBUF) == 0)
  887. panic("msdosfs_rename: no name");
  888. #endif
  889. /*
  890. * Check for cross-device rename.
  891. */
  892. if ((fvp->v_mount != tdvp->v_mount) ||
  893. (tvp && (fvp->v_mount != tvp->v_mount))) {
  894. error = EXDEV;
  895. abortit:
  896. VOP_ABORTOP(tdvp, tcnp);
  897. if (tdvp == tvp)
  898. vrele(tdvp);
  899. else
  900. vput(tdvp);
  901. if (tvp)
  902. vput(tvp);
  903. VOP_ABORTOP(fdvp, fcnp);
  904. vrele(fdvp);
  905. vrele(fvp);
  906. return (error);
  907. }
  908. /*
  909. * If source and dest are the same, do nothing.
  910. */
  911. if (tvp == fvp) {
  912. error = 0;
  913. goto abortit;
  914. }
  915. /* */
  916. if ((error = vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, p)) != 0)
  917. goto abortit;
  918. dp = VTODE(fdvp);
  919. ip = VTODE(fvp);
  920. /*
  921. * Be sure we are not renaming ".", "..", or an alias of ".". This
  922. * leads to a crippled directory tree. It's pretty tough to do a
  923. * "ls" or "pwd" with the "." directory entry missing, and "cd .."
  924. * doesn't work if the ".." entry is missing.
  925. */
  926. if (ip->de_Attributes & ATTR_DIRECTORY) {
  927. /*
  928. * Avoid ".", "..", and aliases of "." for obvious reasons.
  929. */
  930. if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
  931. dp == ip ||
  932. (fcnp->cn_flags & ISDOTDOT) ||
  933. (tcnp->cn_flags & ISDOTDOT) ||
  934. (ip->de_flag & DE_RENAME)) {
  935. VOP_UNLOCK(fvp, 0, p);
  936. error = EINVAL;
  937. goto abortit;
  938. }
  939. ip->de_flag |= DE_RENAME;
  940. doingdirectory++;
  941. }
  942. VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
  943. /*
  944. * When the target exists, both the directory
  945. * and target vnodes are returned locked.
  946. */
  947. dp = VTODE(tdvp);
  948. xp = tvp ? VTODE(tvp) : NULL;
  949. /*
  950. * Remember direntry place to use for destination
  951. */
  952. to_diroffset = dp->de_fndoffset;
  953. to_count = dp->de_fndcnt;
  954. /*
  955. * If ".." must be changed (ie the directory gets a new
  956. * parent) then the source directory must not be in the
  957. * directory hierarchy above the target, as this would
  958. * orphan everything below the source directory. Also
  959. * the user must have write permission in the source so
  960. * as to be able to change "..". We must repeat the call
  961. * to namei, as the parent directory is unlocked by the
  962. * call to doscheckpath().
  963. */
  964. error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
  965. VOP_UNLOCK(fvp, 0, p);
  966. if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
  967. newparent = 1;
  968. vrele(fdvp);
  969. if (doingdirectory && newparent) {
  970. if (error) /* write access check above */
  971. goto bad1;
  972. if (xp != NULL)
  973. vput(tvp);
  974. /*
  975. * doscheckpath() vput()'s dp,
  976. * so we have to do a relookup afterwards
  977. */
  978. if ((error = doscheckpath(ip, dp)) != 0)
  979. goto out;
  980. if ((tcnp->cn_flags & SAVESTART) == 0)
  981. panic("msdosfs_rename: lost to startdir");
  982. if ((error = vfs_relookup(tdvp, &tvp, tcnp)) != 0)
  983. goto out;
  984. dp = VTODE(tdvp);
  985. xp = tvp ? VTODE(tvp) : NULL;
  986. }
  987. VN_KNOTE(tdvp, NOTE_WRITE);
  988. if (xp != NULL) {
  989. /*
  990. * Target must be empty if a directory and have no links
  991. * to it. Also, ensure source and target are compatible
  992. * (both directories, or both not directories).
  993. */
  994. if (xp->de_Attributes & ATTR_DIRECTORY) {
  995. if (!dosdirempty(xp)) {
  996. error = ENOTEMPTY;
  997. goto bad1;
  998. }
  999. if (!doingdirectory) {
  1000. error = ENOTDIR;
  1001. goto bad1;
  1002. }
  1003. cache_purge(tdvp);
  1004. } else if (doingdirectory) {
  1005. error = EISDIR;
  1006. goto bad1;
  1007. }
  1008. if ((error = removede(dp, xp)) != 0)
  1009. goto bad1;
  1010. VN_KNOTE(tvp, NOTE_DELETE);
  1011. vput(tvp);
  1012. xp = NULL;
  1013. }
  1014. /*
  1015. * Convert the filename in tcnp into a dos filename. We copy this
  1016. * into the denode and directory entry for the destination
  1017. * file/directory.
  1018. */
  1019. if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0)
  1020. goto bad1;
  1021. /*
  1022. * Since from wasn't locked at various places above,
  1023. * have to do a relookup here.
  1024. */
  1025. fcnp->cn_flags &= ~MODMASK;
  1026. fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
  1027. if ((fcnp->cn_flags & SAVESTART) == 0)
  1028. panic("msdosfs_rename: lost from startdir");
  1029. if (!newparent)
  1030. VOP_UNLOCK(tdvp, 0, p);
  1031. (void) vfs_relookup(fdvp, &fvp, fcnp);
  1032. if (fvp == NULL) {
  1033. /*
  1034. * From name has disappeared.
  1035. */
  1036. if (doingdirectory)
  1037. panic("rename: lost dir entry");
  1038. vrele(ap->a_fvp);
  1039. if (newparent)
  1040. VOP_UNLOCK(tdvp, 0, p);
  1041. vrele(tdvp);
  1042. return 0;
  1043. }
  1044. xp = VTODE(fvp);
  1045. zp = VTODE(fdvp);
  1046. from_diroffset = zp->de_fndoffset;
  1047. /*
  1048. * Ensure that the directory entry still exists and has not
  1049. * changed till now. If the source is a file the entry may
  1050. * have been unlinked or renamed. In either case there is
  1051. * no further work to be done. If the source is a directory
  1052. * then it cannot have been rmdir'ed or renamed; this is
  1053. * prohibited by the DE_RENAME flag.
  1054. */
  1055. if (xp != ip) {
  1056. if (doingdirectory)
  1057. panic("rename: lost dir entry");
  1058. vrele(ap->a_fvp);
  1059. if (newparent)
  1060. VOP_UNLOCK(fdvp, 0, p);
  1061. xp = NULL;
  1062. } else {
  1063. vrele(fvp);
  1064. xp = NULL;
  1065. /*
  1066. * First write a new entry in the destination
  1067. * directory and mark the entry in the source directory
  1068. * as deleted. Then move the denode to the correct hash
  1069. * chain for its new location in the filesystem. And, if
  1070. * we moved a directory, then update its .. entry to point
  1071. * to the new parent directory.
  1072. */
  1073. bcopy(ip->de_Name, oldname, 11);
  1074. bcopy(toname, ip->de_Name, 11); /* update denode */
  1075. dp->de_fndoffset = to_diroffset;
  1076. dp->de_fndcnt = to_count;
  1077. error = createde(ip, dp, (struct denode **)0, tcnp);
  1078. if (error) {
  1079. bcopy(oldname, ip->de_Name, 11);
  1080. if (newparent)
  1081. VOP_UNLOCK(fdvp, 0, p);
  1082. goto bad;
  1083. }
  1084. ip->de_refcnt++;
  1085. zp->de_fndoffset = from_diroffset;
  1086. if ((error = removede(zp, ip)) != 0) {
  1087. /* XXX should really panic here, fs is corrupt */
  1088. if (newparent)
  1089. VOP_UNLOCK(fdvp, 0, p);
  1090. goto bad;
  1091. }
  1092. cache_purge(fvp);
  1093. if (!doingdirectory) {
  1094. error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
  1095. &ip->de_dirclust, 0);
  1096. if (error) {
  1097. /* XXX should really panic here, fs is corrupt */
  1098. if (newparent)
  1099. VOP_UNLOCK(fdvp, 0, p);
  1100. goto bad;
  1101. }
  1102. if (ip->de_dirclust != MSDOSFSROOT)
  1103. ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
  1104. }
  1105. reinsert(ip);
  1106. if (newparent)
  1107. VOP_UNLOCK(fdvp, 0, p);
  1108. }
  1109. /*
  1110. * If we moved a directory to a new parent directory, then we must
  1111. * fixup the ".." entry in the moved directory.
  1112. */
  1113. if (doingdirectory && newparent) {
  1114. cn = ip->de_StartCluster;
  1115. if (cn == MSDOSFSROOT) {
  1116. /* this should never happen */
  1117. panic("msdosfs_rename: updating .. in root directory?");
  1118. } else
  1119. bn = cntobn(pmp, cn);
  1120. error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
  1121. if (error) {
  1122. /* XXX should really panic here, fs is corrupt */
  1123. brelse(bp);
  1124. goto bad;
  1125. }
  1126. dotdotp = (struct direntry *)bp->b_data;
  1127. putushort(dotdotp[0].deStartCluster, cn);
  1128. pcl = dp->de_StartCluster;
  1129. if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
  1130. pcl = 0;
  1131. putushort(dotdotp[1].deStartCluster, pcl);
  1132. if (FAT32(pmp)) {
  1133. putushort(dotdotp[0].deHighClust, cn >> 16);
  1134. putushort(dotdotp[1].deHighClust, pcl >> 16);
  1135. }
  1136. if ((error = bwrite(bp)) != 0) {
  1137. /* XXX should really panic here, fs is corrupt */
  1138. goto bad;
  1139. }
  1140. }
  1141. VN_KNOTE(fvp, NOTE_RENAME);
  1142. bad:
  1143. VOP_UNLOCK(fvp, 0, p);
  1144. vrele(fdvp);
  1145. bad1:
  1146. if (xp)
  1147. vput(tvp);
  1148. vput(tdvp);
  1149. out:
  1150. ip->de_flag &= ~DE_RENAME;
  1151. vrele(fvp);
  1152. return (error);
  1153. }
  1154. struct {
  1155. struct direntry dot;
  1156. struct direntry dotdot;
  1157. } dosdirtemplate = {
  1158. { ". ", " ", /* the . entry */
  1159. ATTR_DIRECTORY, /* file attribute */
  1160. CASE_LOWER_BASE | CASE_LOWER_EXT, /* lower case */
  1161. 0, /* create time 100ths */
  1162. { 0, 0 }, { 0, 0 }, /* create time & date */
  1163. { 0, 0 }, /* access date */
  1164. { 0, 0 }, /* high bits of start cluster */
  1165. { 210, 4 }, { 210, 4 }, /* modify time & date */
  1166. { 0, 0 }, /* startcluster */
  1167. { 0, 0, 0, 0 } /* filesize */
  1168. },
  1169. { ".. ", " ", /* the .. entry */
  1170. ATTR_DIRECTORY, /* file attribute */
  1171. CASE_LOWER_BASE | CASE_LOWER_EXT, /* lower case */
  1172. 0, /* create time 100ths */
  1173. { 0, 0 }, { 0, 0 }, /* create time & date */
  1174. { 0, 0 }, /* access date */
  1175. { 0, 0 }, /* high bits of start cluster */
  1176. { 210, 4 }, { 210, 4 }, /* modify time & date */
  1177. { 0, 0 }, /* startcluster */
  1178. { 0, 0, 0, 0 } /* filesize */
  1179. }
  1180. };
  1181. int
  1182. msdosfs_mkdir(void *v)
  1183. {
  1184. struct vop_mkdir_args *ap = v;
  1185. struct componentname *cnp = ap->a_cnp;
  1186. struct denode ndirent;
  1187. struct denode *dep;
  1188. struct denode *pdep = VTODE(ap->a_dvp);
  1189. int error;
  1190. daddr_t bn;
  1191. uint32_t newcluster, pcl;
  1192. struct direntry *denp;
  1193. struct msdosfsmount *pmp = pdep->de_pmp;
  1194. struct buf *bp;
  1195. struct timespec ts;
  1196. /*
  1197. * If this is the root directory and there is no space left we
  1198. * can't do anything. This is because the root directory can not
  1199. * change size.
  1200. */
  1201. if (pdep->de_StartCluster == MSDOSFSROOT
  1202. && pdep->de_fndoffset >= pdep->de_FileSize) {
  1203. error = ENOSPC;
  1204. goto bad2;
  1205. }
  1206. /*
  1207. * Allocate a cluster to hold the about to be created directory.
  1208. */
  1209. error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
  1210. if (error)
  1211. goto bad2;
  1212. bzero(&ndirent, sizeof(ndirent));
  1213. ndirent.de_pmp = pmp;
  1214. ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
  1215. getnanotime(&ts);
  1216. DETIMES(&ndirent, &ts, &ts, &ts);
  1217. /*
  1218. * Now fill the cluster with the "." and ".." entries. And write
  1219. * the cluster to disk. This way it is there for the parent
  1220. * directory to be pointing at if there were a crash.
  1221. */
  1222. bn = cntobn(pmp, newcluster);
  1223. /* always succeeds */
  1224. bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
  1225. bzero(bp->b_data, pmp->pm_bpcluster);
  1226. bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
  1227. denp = (struct direntry *)bp->b_data;
  1228. putushort(denp[0].deStartCluster, newcluster);
  1229. putushort(denp[0].deCDate, ndirent.de_CDate);
  1230. putushort(denp[0].deCTime, ndirent.de_CTime);
  1231. denp[0].deCTimeHundredth = ndirent.de_CTimeHundredth;
  1232. putushort(denp[0].deADate, ndirent.de_ADate);
  1233. putushort(denp[0].deMDate, ndirent.de_MDate);
  1234. putushort(denp[0].deMTime, ndirent.de_MTime);
  1235. pcl = pdep->de_StartCluster;
  1236. if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
  1237. pcl = 0;
  1238. putushort(denp[1].deStartCluster, pcl);
  1239. putushort(denp[1].deCDate, ndirent.de_CDate);
  1240. putushort(denp[1].deCTime, ndirent.de_CTime);
  1241. denp[1].deCTimeHundredth = ndirent.de_CTimeHundredth;
  1242. putushort(denp[1].deADate, ndirent.de_ADate);
  1243. putushort(denp[1].deMDate, ndirent.de_MDate);
  1244. putushort(denp[1].deMTime, ndirent.de_MTime);
  1245. if (FAT32(pmp)) {
  1246. putushort(denp[0].deHighClust, newcluster >> 16);
  1247. putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
  1248. }
  1249. if ((error = bwrite(bp)) != 0)
  1250. goto bad;
  1251. /*
  1252. * Now build up a directory entry pointing to the newly allocated
  1253. * cluster. This will be written to an empty slot in the parent
  1254. * directory.
  1255. */
  1256. #ifdef DIAGNOSTIC
  1257. if ((cnp->cn_flags & HASBUF) == 0)
  1258. panic("msdosfs_mkdir: no name");
  1259. #endif
  1260. if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
  1261. goto bad;
  1262. ndirent.de_Attributes = ATTR_DIRECTORY;
  1263. ndirent.de_StartCluster = newcluster;
  1264. ndirent.de_FileSize = 0;
  1265. ndirent.de_dev = pdep->de_dev;
  1266. ndirent.de_devvp = pdep->de_devvp;
  1267. if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
  1268. goto bad;
  1269. if ((cnp->cn_flags & SAVESTART) == 0)
  1270. pool_put(&namei_pool, cnp->cn_pnbuf);
  1271. VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
  1272. vput(ap->a_dvp);
  1273. *ap->a_vpp = DETOV(dep);
  1274. return (0);
  1275. bad:
  1276. clusterfree(pmp, newcluster, NULL);
  1277. bad2:
  1278. pool_put(&namei_pool, cnp->cn_pnbuf);
  1279. vput(ap->a_dvp);
  1280. return (error);
  1281. }
  1282. int
  1283. msdosfs_rmdir(void *v)
  1284. {
  1285. struct vop_rmdir_args *ap = v;
  1286. struct vnode *vp = ap->a_vp;
  1287. struct vnode *dvp = ap->a_dvp;
  1288. struct componentname *cnp = ap->a_cnp;
  1289. struct denode *ip, *dp;
  1290. int error;
  1291. ip = VTODE(vp);
  1292. dp = VTODE(dvp);
  1293. /*
  1294. * No rmdir "." please.
  1295. */
  1296. if (dp == ip) {
  1297. vrele(dvp);
  1298. vput(vp);
  1299. return (EINVAL);
  1300. }
  1301. /*
  1302. * Verify the directory is empty (and valid).
  1303. * (Rmdir ".." won't be valid since
  1304. * ".." will contain a reference to
  1305. * the current directory and thus be
  1306. * non-empty.)
  1307. */
  1308. error = 0;
  1309. if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
  1310. error = ENOTEMPTY;
  1311. goto out;
  1312. }
  1313. VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
  1314. /*
  1315. * Delete the entry from the directory. For dos filesystems this
  1316. * gets rid of the directory entry on disk, the in memory copy
  1317. * still exists but the de_refcnt is <= 0. This prevents it from
  1318. * being found by deget(). When the vput() on dep is done we give
  1319. * up access and eventually msdosfs_reclaim() will be called which
  1320. * will remove it from the denode cache.
  1321. */
  1322. if ((error = removede(dp, ip)) != 0)
  1323. goto out;
  1324. /*
  1325. * This is where we decrement the link count in the parent
  1326. * directory. Since dos filesystems don't do this we just purge
  1327. * the name cache and let go of the parent directory denode.
  1328. */
  1329. cache_purge(dvp);
  1330. vput(dvp);
  1331. dvp = NULL;
  1332. /*
  1333. * Truncate the directory that is being deleted.
  1334. */
  1335. error = detrunc(ip, (uint32_t)0, IO_SYNC, cnp->cn_cred, cnp->cn_proc);
  1336. cache_purge(vp);
  1337. out:
  1338. if (dvp)
  1339. vput(dvp);
  1340. VN_KNOTE(vp, NOTE_DELETE);
  1341. vput(vp);
  1342. return (error);
  1343. }
  1344. /*
  1345. * DOS filesystems don't know what symlinks are.
  1346. */
  1347. int
  1348. msdosfs_symlink(void *v)
  1349. {
  1350. struct vop_symlink_args *ap = v;
  1351. VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
  1352. vput(ap->a_dvp);
  1353. return (EOPNOTSUPP);
  1354. }
  1355. int
  1356. msdosfs_readdir(void *v)
  1357. {
  1358. struct vop_readdir_args *ap = v;
  1359. int error = 0;
  1360. int diff;
  1361. long n;
  1362. int blsize;
  1363. long on;
  1364. long lost;
  1365. long count;
  1366. uint32_t dirsperblk;
  1367. uint32_t cn, lbn;
  1368. uint32_t fileno;
  1369. long bias = 0;
  1370. daddr_t bn;
  1371. struct buf *bp;
  1372. struct denode *dep = VTODE(ap->a_vp);
  1373. struct msdosfsmount *pmp = dep->de_pmp;
  1374. struct direntry *dentp;
  1375. struct dirent dirbuf;
  1376. struct uio *uio = ap->a_uio;
  1377. off_t offset, wlast = -1;
  1378. int chksum = -1;
  1379. #ifdef MSDOSFS_DEBUG
  1380. printf("msdosfs_readdir(): vp %08x, uio %08x, cred %08x, eofflagp %08x\n",
  1381. ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
  1382. #endif
  1383. /*
  1384. * msdosfs_readdir() won't operate properly on regular files since
  1385. * it does i/o only with the filesystem vnode, and hence can
  1386. * retrieve the wrong block from the buffer cache for a plain file.
  1387. * So, fail attempts to readdir() on a plain file.
  1388. */
  1389. if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
  1390. return (ENOTDIR);
  1391. /*
  1392. * To be safe, initialize dirbuf
  1393. */
  1394. bzero(&dirbuf, sizeof(dirbuf));
  1395. /*
  1396. * If the user buffer is smaller than the size of one dos directory
  1397. * entry or the file offset is not a multiple of the size of a
  1398. * directory entry, then we fail the read.
  1399. */
  1400. count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
  1401. offset = uio->uio_offset;
  1402. if (count < sizeof(struct direntry) ||
  1403. (offset & (sizeof(struct direntry) - 1)))
  1404. return (EINVAL);
  1405. lost = uio->uio_resid - count;
  1406. uio->uio_resid = count;
  1407. dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
  1408. /*
  1409. * If they are reading from the root directory then, we simulate
  1410. * the . and .. entries since these don't exist in the root
  1411. * directory. We also set the offset bias to make up for having to
  1412. * simulate these entries. By this I mean that at file offset 64 we
  1413. * read the first entry in the root directory that lives on disk.
  1414. */
  1415. if (dep->de_StartCluster == MSDOSFSROOT
  1416. || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
  1417. #if 0
  1418. printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
  1419. offset);
  1420. #endif
  1421. bias = 2 * sizeof(struct direntry);
  1422. if (offset < bias) {
  1423. for (n = (int)offset / sizeof(struct direntry);
  1424. n < 2; n++) {
  1425. if (FAT32(pmp))
  1426. dirbuf.d_fileno = pmp->pm_rootdirblk;
  1427. else
  1428. dirbuf.d_fileno = 1;
  1429. dirbuf.d_type = DT_DIR;
  1430. switch (n) {
  1431. case 0:
  1432. dirbuf.d_namlen = 1;
  1433. strlcpy(dirbuf.d_name, ".",
  1434. sizeof dirbuf.d_name);
  1435. break;
  1436. case 1:
  1437. dirbuf.d_namlen = 2;
  1438. strlcpy(dirbuf.d_name, "..",
  1439. sizeof dirbuf.d_name);
  1440. break;
  1441. }
  1442. dirbuf.d_reclen = DIRENT_SIZE(&dirbuf);
  1443. dirbuf.d_off = offset +
  1444. sizeof(struct direntry);
  1445. if (uio->uio_resid < dirbuf.d_reclen)
  1446. goto out;
  1447. error = uiomovei(&dirbuf, dirbuf.d_reclen, uio);
  1448. if (error)
  1449. goto out;
  1450. offset = dirbuf.d_off;
  1451. }
  1452. }
  1453. }
  1454. while (uio->uio_resid > 0) {
  1455. lbn = de_cluster(pmp, offset - bias);
  1456. on = (offset - bias) & pmp->pm_crbomask;
  1457. n = min(pmp->pm_bpcluster - on, uio->uio_resid);
  1458. diff = dep->de_FileSize - (offset - bias);
  1459. if (diff <= 0)
  1460. break;
  1461. n = min(n, diff);
  1462. if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
  1463. break;
  1464. error = bread(pmp->pm_devvp, bn, blsize, &bp);
  1465. if (error) {
  1466. brelse(bp);
  1467. return (error);
  1468. }
  1469. n = min(n, blsize - bp->b_resid);
  1470. /*
  1471. * Convert from dos directory entries to fs-independent
  1472. * directory entries.
  1473. */
  1474. for (dentp = (struct direntry *)(bp->b_data + on);
  1475. (char *)dentp < bp->b_data + on + n;
  1476. dentp++, offset += sizeof(struct direntry)) {
  1477. #if 0
  1478. printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
  1479. dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
  1480. #endif
  1481. /*
  1482. * If this is an unused entry, we can stop.
  1483. */
  1484. if (dentp->deName[0] == SLOT_EMPTY) {
  1485. brelse(bp);
  1486. goto out;
  1487. }
  1488. /*
  1489. * Skip deleted entries.
  1490. */
  1491. if (dentp->deName[0] == SLOT_DELETED) {
  1492. chksum = -1;
  1493. wlast = -1;
  1494. continue;
  1495. }
  1496. /*
  1497. * Handle Win95 long directory entries
  1498. */
  1499. if (dentp->deAttributes == ATTR_WIN95) {
  1500. struct winentry *wep;
  1501. if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
  1502. continue;
  1503. wep = (struct winentry *)dentp;
  1504. chksum = win2unixfn(wep, &dirbuf, chksum);
  1505. if (wep->weCnt & WIN_LAST)
  1506. wlast = offset;
  1507. continue;
  1508. }
  1509. /*
  1510. * Skip volume labels
  1511. */
  1512. if (dentp->deAttributes & ATTR_VOLUME) {
  1513. chksum = -1;
  1514. wlast = -1;
  1515. continue;
  1516. }
  1517. /*
  1518. * This computation of d_fileno must match
  1519. * the computation of va_fileid in
  1520. * msdosfs_getattr.
  1521. */
  1522. fileno = getushort(dentp->deStartCluster);
  1523. if (FAT32(pmp))
  1524. fileno |= getushort(dentp->deHighClust) << 16;
  1525. if (dentp->deAttributes & ATTR_DIRECTORY) {
  1526. /* Special-case root */
  1527. if (fileno == MSDOSFSROOT) {
  1528. fileno = FAT32(pmp) ?
  1529. pmp->pm_rootdirblk : 1;
  1530. }
  1531. dirbuf.d_fileno = fileno;
  1532. dirbuf.d_type = DT_DIR;
  1533. } else {
  1534. if (getulong(dentp->deFileSize) == 0) {
  1535. uint64_t fileno64;
  1536. fileno64 = (cn == MSDOSFSROOT) ?
  1537. roottobn(pmp, 0) : cntobn(pmp, cn);
  1538. fileno64 *= dirsperblk;
  1539. fileno64 += dentp -
  1540. (struct direntry *)bp->b_data;
  1541. fileno = fileidhash(fileno64);
  1542. }
  1543. dirbuf.d_fileno = fileno;
  1544. dirbuf.d_type = DT_REG;
  1545. }
  1546. if (chksum != winChksum(dentp->deName))
  1547. dirbuf.d_namlen = dos2unixfn(dentp->deName,
  1548. (u_char *)dirbuf.d_name,
  1549. pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
  1550. else
  1551. dirbuf.d_name[dirbuf.d_namlen] = 0;
  1552. chksum = -1;
  1553. dirbuf.d_reclen = DIRENT_SIZE(&dirbuf);
  1554. dirbuf.d_off = offset + sizeof(struct direntry);
  1555. if (uio->uio_resid < dirbuf.d_reclen) {
  1556. brelse(bp);
  1557. /* Remember long-name offset. */
  1558. if (wlast != -1)
  1559. offset = wlast;
  1560. goto out;
  1561. }
  1562. wlast = -1;
  1563. error = uiomovei(&dirbuf, dirbuf.d_reclen, uio);
  1564. if (error) {
  1565. brelse(bp);
  1566. goto out;
  1567. }
  1568. }
  1569. brelse(bp);
  1570. }
  1571. out:
  1572. uio->uio_offset = offset;
  1573. uio->uio_resid += lost;
  1574. if (dep->de_FileSize - (offset - bias) <= 0)
  1575. *ap->a_eofflag = 1;
  1576. else
  1577. *ap->a_eofflag = 0;
  1578. return (error);
  1579. }
  1580. /*
  1581. * DOS filesystems don't know what symlinks are.
  1582. */
  1583. int
  1584. msdosfs_readlink(void *v)
  1585. {
  1586. #if 0
  1587. struct vop_readlink_args /* {
  1588. struct vnode *a_vp;
  1589. struct uio *a_uio;
  1590. struct ucred *a_cred;
  1591. } */ *ap;
  1592. #endif
  1593. return (EINVAL);
  1594. }
  1595. int
  1596. msdosfs_lock(void *v)
  1597. {
  1598. struct vop_lock_args *ap = v;
  1599. struct vnode *vp = ap->a_vp;
  1600. return (lockmgr(&VTODE(vp)->de_lock, ap->a_flags, NULL));
  1601. }
  1602. int
  1603. msdosfs_unlock(void *v)
  1604. {
  1605. struct vop_unlock_args *ap = v;
  1606. struct vnode *vp = ap->a_vp;
  1607. return (lockmgr(&VTODE(vp)->de_lock, ap->a_flags | LK_RELEASE, NULL));
  1608. }
  1609. int
  1610. msdosfs_islocked(void *v)
  1611. {
  1612. struct vop_islocked_args *ap = v;
  1613. return (lockstatus(&VTODE(ap->a_vp)->de_lock));
  1614. }
  1615. /*
  1616. * vp - address of vnode file the file
  1617. * bn - which cluster we are interested in mapping to a filesystem block number.
  1618. * vpp - returns the vnode for the block special file holding the filesystem
  1619. * containing the file of interest
  1620. * bnp - address of where to return the filesystem relative block number
  1621. */
  1622. int
  1623. msdosfs_bmap(void *v)
  1624. {
  1625. struct vop_bmap_args *ap = v;
  1626. struct denode *dep = VTODE(ap->a_vp);
  1627. struct msdosfsmount *pmp = dep->de_pmp;
  1628. if (ap->a_vpp != NULL)
  1629. *ap->a_vpp = dep->de_devvp;
  1630. if (ap->a_bnp == NULL)
  1631. return (0);
  1632. if (ap->a_runp) {
  1633. /*
  1634. * Sequential clusters should be counted here.
  1635. */
  1636. *ap->a_runp = 0;
  1637. }
  1638. return (pcbmap(dep, de_bn2cn(pmp, ap->a_bn), ap->a_bnp, 0, 0));
  1639. }
  1640. int
  1641. msdosfs_strategy(void *v)
  1642. {
  1643. struct vop_strategy_args *ap = v;
  1644. struct buf *bp = ap->a_bp;
  1645. struct denode *dep = VTODE(bp->b_vp);
  1646. struct vnode *vp;
  1647. int error = 0;
  1648. int s;
  1649. if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
  1650. panic("msdosfs_strategy: spec");
  1651. /*
  1652. * If we don't already know the filesystem relative block number
  1653. * then get it using pcbmap(). If pcbmap() returns the block
  1654. * number as -1 then we've got a hole in the file. DOS filesystems
  1655. * don't allow files with holes, so we shouldn't ever see this.
  1656. */
  1657. if (bp->b_blkno == bp->b_lblkno) {
  1658. error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
  1659. &bp->b_blkno, 0, 0);
  1660. if (error)
  1661. bp->b_blkno = -1;
  1662. if (bp->b_blkno == -1)
  1663. clrbuf(bp);
  1664. }
  1665. if (bp->b_blkno == -1) {
  1666. s = splbio();
  1667. biodone(bp);
  1668. splx(s);
  1669. return (error);
  1670. }
  1671. /*
  1672. * Read/write the block from/to the disk that contains the desired
  1673. * file block.
  1674. */
  1675. vp = dep->de_devvp;
  1676. bp->b_dev = vp->v_rdev;
  1677. (vp->v_op->vop_strategy)(ap);
  1678. return (0);
  1679. }
  1680. int
  1681. msdosfs_print(void *v)
  1682. {
  1683. struct vop_print_args *ap = v;
  1684. struct denode *dep = VTODE(ap->a_vp);
  1685. printf(
  1686. "tag VT_MSDOSFS, startcluster %u, dircluster %u, diroffset %u ",
  1687. dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
  1688. printf(" dev %d, %d, %s\n",
  1689. major(dep->de_dev), minor(dep->de_dev),
  1690. VOP_ISLOCKED(ap->a_vp) ? "(LOCKED)" : "");
  1691. #ifdef DIAGNOSTIC
  1692. lockmgr_printinfo(&dep->de_lock);
  1693. printf("\n");
  1694. #endif
  1695. return (0);
  1696. }
  1697. int
  1698. msdosfs_advlock(void *v)
  1699. {
  1700. struct vop_advlock_args *ap = v;
  1701. struct denode *dep = VTODE(ap->a_vp);
  1702. return (lf_advlock(&dep->de_lockf, dep->de_FileSize, ap->a_id, ap->a_op,
  1703. ap->a_fl, ap->a_flags));
  1704. }
  1705. int
  1706. msdosfs_pathconf(void *v)
  1707. {
  1708. struct vop_pathconf_args *ap = v;
  1709. struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
  1710. int error = 0;
  1711. switch (ap->a_name) {
  1712. case _PC_LINK_MAX:
  1713. *ap->a_retval = 1;
  1714. break;
  1715. case _PC_NAME_MAX:
  1716. *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
  1717. break;
  1718. case _PC_CHOWN_RESTRICTED:
  1719. *ap->a_retval = 1;
  1720. break;
  1721. case _PC_NO_TRUNC:
  1722. *ap->a_retval = 0;
  1723. break;
  1724. case _PC_TIMESTAMP_RESOLUTION:
  1725. *ap->a_retval = 2000000000; /* 2 billion nanoseconds */
  1726. break;
  1727. default:
  1728. error = EINVAL;
  1729. break;
  1730. }
  1731. return (error);
  1732. }
  1733. /*
  1734. * Thomas Wang's hash function, severely hacked to always set the high
  1735. * bit on the number it returns (so no longer a proper hash function).
  1736. */
  1737. static uint32_t
  1738. fileidhash(uint64_t fileid)
  1739. {
  1740. uint64_t c1 = 0x6e5ea73858134343LL;
  1741. uint64_t c2 = 0xb34e8f99a2ec9ef5LL;
  1742. /*
  1743. * We now have the original fileid value, as 64-bit value.
  1744. * We need to reduce it to 32-bits, with the top bit set.
  1745. */
  1746. fileid ^= ((c1 ^ fileid) >> 32);
  1747. fileid *= c1;
  1748. fileid ^= ((c2 ^ fileid) >> 31);
  1749. fileid *= c2;
  1750. fileid ^= ((c1 ^ fileid) >> 32);
  1751. return (uint32_t)(fileid | 0x80000000);
  1752. }
  1753. /* Global vfs data structures for msdosfs */
  1754. struct vops msdosfs_vops = {
  1755. .vop_lookup = msdosfs_lookup,
  1756. .vop_create = msdosfs_create,
  1757. .vop_mknod = msdosfs_mknod,
  1758. .vop_open = msdosfs_open,
  1759. .vop_close = msdosfs_close,
  1760. .vop_access = msdosfs_access,
  1761. .vop_getattr = msdosfs_getattr,
  1762. .vop_setattr = msdosfs_setattr,
  1763. .vop_read = msdosfs_read,
  1764. .vop_write = msdosfs_write,
  1765. .vop_ioctl = msdosfs_ioctl,
  1766. .vop_poll = msdosfs_poll,
  1767. .vop_kqfilter = msdosfs_kqfilter,
  1768. .vop_fsync = msdosfs_fsync,
  1769. .vop_remove = msdosfs_remove,
  1770. .vop_link = msdosfs_link,
  1771. .vop_rename = msdosfs_rename,
  1772. .vop_mkdir = msdosfs_mkdir,
  1773. .vop_rmdir = msdosfs_rmdir,
  1774. .vop_symlink = msdosfs_symlink,
  1775. .vop_readdir = msdosfs_readdir,
  1776. .vop_readlink = msdosfs_readlink,
  1777. .vop_abortop = vop_generic_abortop,
  1778. .vop_inactive = msdosfs_inactive,
  1779. .vop_reclaim = msdosfs_reclaim,
  1780. .vop_lock = msdosfs_lock,
  1781. .vop_unlock = msdosfs_unlock,
  1782. .vop_bmap = msdosfs_bmap,
  1783. .vop_strategy = msdosfs_strategy,
  1784. .vop_print = msdosfs_print,
  1785. .vop_islocked = msdosfs_islocked,
  1786. .vop_pathconf = msdosfs_pathconf,
  1787. .vop_advlock = msdosfs_advlock,
  1788. .vop_bwrite = vop_generic_bwrite,
  1789. .vop_revoke = vop_generic_revoke,
  1790. };
  1791. struct filterops msdosfsread_filtops =
  1792. { 1, NULL, filt_msdosfsdetach, filt_msdosfsread };
  1793. struct filterops msdosfswrite_filtops =
  1794. { 1, NULL, filt_msdosfsdetach, filt_msdosfswrite };
  1795. struct filterops msdosfsvnode_filtops =
  1796. { 1, NULL, filt_msdosfsdetach, filt_msdosfsvnode };
  1797. int
  1798. msdosfs_kqfilter(void *v)
  1799. {
  1800. struct vop_kqfilter_args *ap = v;
  1801. struct vnode *vp = ap->a_vp;
  1802. struct knote *kn = ap->a_kn;
  1803. switch (kn->kn_filter) {
  1804. case EVFILT_READ:
  1805. kn->kn_fop = &msdosfsread_filtops;
  1806. break;
  1807. case EVFILT_WRITE:
  1808. kn->kn_fop = &msdosfswrite_filtops;
  1809. break;
  1810. case EVFILT_VNODE:
  1811. kn->kn_fop = &msdosfsvnode_filtops;
  1812. break;
  1813. default:
  1814. return (EINVAL);
  1815. }
  1816. kn->kn_hook = (caddr_t)vp;
  1817. SLIST_INSERT_HEAD(&vp->v_selectinfo.si_note, kn, kn_selnext);
  1818. return (0);
  1819. }
  1820. void
  1821. filt_msdosfsdetach(struct knote *kn)
  1822. {
  1823. struct vnode *vp = (struct vnode *)kn->kn_hook;
  1824. SLIST_REMOVE(&vp->v_selectinfo.si_note, kn, knote, kn_selnext);
  1825. }
  1826. int
  1827. filt_msdosfsread(struct knote *kn, long hint)
  1828. {
  1829. struct vnode *vp = (struct vnode *)kn->kn_hook;
  1830. struct denode *dep = VTODE(vp);
  1831. /*
  1832. * filesystem is gone, so set the EOF flag and schedule
  1833. * the knote for deletion.
  1834. */
  1835. if (hint == NOTE_REVOKE) {
  1836. kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  1837. return (1);
  1838. }
  1839. kn->kn_data = dep->de_FileSize - kn->kn_fp->f_offset;
  1840. if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
  1841. kn->kn_fflags |= NOTE_EOF;
  1842. return (1);
  1843. }
  1844. return (kn->kn_data != 0);
  1845. }
  1846. int
  1847. filt_msdosfswrite(struct knote *kn, long hint)
  1848. {
  1849. /*
  1850. * filesystem is gone, so set the EOF flag and schedule
  1851. * the knote for deletion.
  1852. */
  1853. if (hint == NOTE_REVOKE) {
  1854. kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  1855. return (1);
  1856. }
  1857. kn->kn_data = 0;
  1858. return (1);
  1859. }
  1860. int
  1861. filt_msdosfsvnode(struct knote *kn, long hint)
  1862. {
  1863. if (kn->kn_sfflags & hint)
  1864. kn->kn_fflags |= hint;
  1865. if (hint == NOTE_REVOKE) {
  1866. kn->kn_flags |= EV_EOF;
  1867. return (1);
  1868. }
  1869. return (kn->kn_fflags != 0);
  1870. }