xfs_ioctl.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_rtalloc.h"
  15. #include "xfs_iwalk.h"
  16. #include "xfs_itable.h"
  17. #include "xfs_error.h"
  18. #include "xfs_da_format.h"
  19. #include "xfs_da_btree.h"
  20. #include "xfs_attr.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_bmap_util.h"
  23. #include "xfs_fsops.h"
  24. #include "xfs_discard.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_export.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_icache.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_acl.h"
  31. #include "xfs_btree.h"
  32. #include <linux/fsmap.h>
  33. #include "xfs_fsmap.h"
  34. #include "scrub/xfs_scrub.h"
  35. #include "xfs_sb.h"
  36. #include "xfs_ag.h"
  37. #include "xfs_health.h"
  38. #include "xfs_reflink.h"
  39. #include "xfs_ioctl.h"
  40. #include "xfs_xattr.h"
  41. #include <linux/mount.h>
  42. #include <linux/namei.h>
  43. #include <linux/fileattr.h>
  44. /*
  45. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  46. * a file or fs handle.
  47. *
  48. * XFS_IOC_PATH_TO_FSHANDLE
  49. * returns fs handle for a mount point or path within that mount point
  50. * XFS_IOC_FD_TO_HANDLE
  51. * returns full handle for a FD opened in user space
  52. * XFS_IOC_PATH_TO_HANDLE
  53. * returns full handle for a path
  54. */
  55. int
  56. xfs_find_handle(
  57. unsigned int cmd,
  58. xfs_fsop_handlereq_t *hreq)
  59. {
  60. int hsize;
  61. xfs_handle_t handle;
  62. struct inode *inode;
  63. struct fd f = {NULL};
  64. struct path path;
  65. int error;
  66. struct xfs_inode *ip;
  67. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  68. f = fdget(hreq->fd);
  69. if (!f.file)
  70. return -EBADF;
  71. inode = file_inode(f.file);
  72. } else {
  73. error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
  74. if (error)
  75. return error;
  76. inode = d_inode(path.dentry);
  77. }
  78. ip = XFS_I(inode);
  79. /*
  80. * We can only generate handles for inodes residing on a XFS filesystem,
  81. * and only for regular files, directories or symbolic links.
  82. */
  83. error = -EINVAL;
  84. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  85. goto out_put;
  86. error = -EBADF;
  87. if (!S_ISREG(inode->i_mode) &&
  88. !S_ISDIR(inode->i_mode) &&
  89. !S_ISLNK(inode->i_mode))
  90. goto out_put;
  91. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  92. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  93. /*
  94. * This handle only contains an fsid, zero the rest.
  95. */
  96. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  97. hsize = sizeof(xfs_fsid_t);
  98. } else {
  99. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  100. sizeof(handle.ha_fid.fid_len);
  101. handle.ha_fid.fid_pad = 0;
  102. handle.ha_fid.fid_gen = inode->i_generation;
  103. handle.ha_fid.fid_ino = ip->i_ino;
  104. hsize = sizeof(xfs_handle_t);
  105. }
  106. error = -EFAULT;
  107. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  108. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  109. goto out_put;
  110. error = 0;
  111. out_put:
  112. if (cmd == XFS_IOC_FD_TO_HANDLE)
  113. fdput(f);
  114. else
  115. path_put(&path);
  116. return error;
  117. }
  118. /*
  119. * No need to do permission checks on the various pathname components
  120. * as the handle operations are privileged.
  121. */
  122. STATIC int
  123. xfs_handle_acceptable(
  124. void *context,
  125. struct dentry *dentry)
  126. {
  127. return 1;
  128. }
  129. /*
  130. * Convert userspace handle data into a dentry.
  131. */
  132. struct dentry *
  133. xfs_handle_to_dentry(
  134. struct file *parfilp,
  135. void __user *uhandle,
  136. u32 hlen)
  137. {
  138. xfs_handle_t handle;
  139. struct xfs_fid64 fid;
  140. /*
  141. * Only allow handle opens under a directory.
  142. */
  143. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  144. return ERR_PTR(-ENOTDIR);
  145. if (hlen != sizeof(xfs_handle_t))
  146. return ERR_PTR(-EINVAL);
  147. if (copy_from_user(&handle, uhandle, hlen))
  148. return ERR_PTR(-EFAULT);
  149. if (handle.ha_fid.fid_len !=
  150. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  151. return ERR_PTR(-EINVAL);
  152. memset(&fid, 0, sizeof(struct fid));
  153. fid.ino = handle.ha_fid.fid_ino;
  154. fid.gen = handle.ha_fid.fid_gen;
  155. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  156. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  157. xfs_handle_acceptable, NULL);
  158. }
  159. STATIC struct dentry *
  160. xfs_handlereq_to_dentry(
  161. struct file *parfilp,
  162. xfs_fsop_handlereq_t *hreq)
  163. {
  164. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  165. }
  166. int
  167. xfs_open_by_handle(
  168. struct file *parfilp,
  169. xfs_fsop_handlereq_t *hreq)
  170. {
  171. const struct cred *cred = current_cred();
  172. int error;
  173. int fd;
  174. int permflag;
  175. struct file *filp;
  176. struct inode *inode;
  177. struct dentry *dentry;
  178. fmode_t fmode;
  179. struct path path;
  180. if (!capable(CAP_SYS_ADMIN))
  181. return -EPERM;
  182. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  183. if (IS_ERR(dentry))
  184. return PTR_ERR(dentry);
  185. inode = d_inode(dentry);
  186. /* Restrict xfs_open_by_handle to directories & regular files. */
  187. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  188. error = -EPERM;
  189. goto out_dput;
  190. }
  191. #if BITS_PER_LONG != 32
  192. hreq->oflags |= O_LARGEFILE;
  193. #endif
  194. permflag = hreq->oflags;
  195. fmode = OPEN_FMODE(permflag);
  196. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  197. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  198. error = -EPERM;
  199. goto out_dput;
  200. }
  201. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  202. error = -EPERM;
  203. goto out_dput;
  204. }
  205. /* Can't write directories. */
  206. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  207. error = -EISDIR;
  208. goto out_dput;
  209. }
  210. fd = get_unused_fd_flags(0);
  211. if (fd < 0) {
  212. error = fd;
  213. goto out_dput;
  214. }
  215. path.mnt = parfilp->f_path.mnt;
  216. path.dentry = dentry;
  217. filp = dentry_open(&path, hreq->oflags, cred);
  218. dput(dentry);
  219. if (IS_ERR(filp)) {
  220. put_unused_fd(fd);
  221. return PTR_ERR(filp);
  222. }
  223. if (S_ISREG(inode->i_mode)) {
  224. filp->f_flags |= O_NOATIME;
  225. filp->f_mode |= FMODE_NOCMTIME;
  226. }
  227. fd_install(fd, filp);
  228. return fd;
  229. out_dput:
  230. dput(dentry);
  231. return error;
  232. }
  233. int
  234. xfs_readlink_by_handle(
  235. struct file *parfilp,
  236. xfs_fsop_handlereq_t *hreq)
  237. {
  238. struct dentry *dentry;
  239. __u32 olen;
  240. int error;
  241. if (!capable(CAP_SYS_ADMIN))
  242. return -EPERM;
  243. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  244. if (IS_ERR(dentry))
  245. return PTR_ERR(dentry);
  246. /* Restrict this handle operation to symlinks only. */
  247. if (!d_is_symlink(dentry)) {
  248. error = -EINVAL;
  249. goto out_dput;
  250. }
  251. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  252. error = -EFAULT;
  253. goto out_dput;
  254. }
  255. error = vfs_readlink(dentry, hreq->ohandle, olen);
  256. out_dput:
  257. dput(dentry);
  258. return error;
  259. }
  260. /*
  261. * Format an attribute and copy it out to the user's buffer.
  262. * Take care to check values and protect against them changing later,
  263. * we may be reading them directly out of a user buffer.
  264. */
  265. static void
  266. xfs_ioc_attr_put_listent(
  267. struct xfs_attr_list_context *context,
  268. int flags,
  269. unsigned char *name,
  270. int namelen,
  271. int valuelen)
  272. {
  273. struct xfs_attrlist *alist = context->buffer;
  274. struct xfs_attrlist_ent *aep;
  275. int arraytop;
  276. ASSERT(!context->seen_enough);
  277. ASSERT(context->count >= 0);
  278. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  279. ASSERT(context->firstu >= sizeof(*alist));
  280. ASSERT(context->firstu <= context->bufsize);
  281. /*
  282. * Only list entries in the right namespace.
  283. */
  284. if (context->attr_filter != (flags & XFS_ATTR_NSP_ONDISK_MASK))
  285. return;
  286. arraytop = sizeof(*alist) +
  287. context->count * sizeof(alist->al_offset[0]);
  288. /* decrement by the actual bytes used by the attr */
  289. context->firstu -= round_up(offsetof(struct xfs_attrlist_ent, a_name) +
  290. namelen + 1, sizeof(uint32_t));
  291. if (context->firstu < arraytop) {
  292. trace_xfs_attr_list_full(context);
  293. alist->al_more = 1;
  294. context->seen_enough = 1;
  295. return;
  296. }
  297. aep = context->buffer + context->firstu;
  298. aep->a_valuelen = valuelen;
  299. memcpy(aep->a_name, name, namelen);
  300. aep->a_name[namelen] = 0;
  301. alist->al_offset[context->count++] = context->firstu;
  302. alist->al_count = context->count;
  303. trace_xfs_attr_list_add(context);
  304. }
  305. static unsigned int
  306. xfs_attr_filter(
  307. u32 ioc_flags)
  308. {
  309. if (ioc_flags & XFS_IOC_ATTR_ROOT)
  310. return XFS_ATTR_ROOT;
  311. if (ioc_flags & XFS_IOC_ATTR_SECURE)
  312. return XFS_ATTR_SECURE;
  313. return 0;
  314. }
  315. static unsigned int
  316. xfs_attr_flags(
  317. u32 ioc_flags)
  318. {
  319. if (ioc_flags & XFS_IOC_ATTR_CREATE)
  320. return XATTR_CREATE;
  321. if (ioc_flags & XFS_IOC_ATTR_REPLACE)
  322. return XATTR_REPLACE;
  323. return 0;
  324. }
  325. int
  326. xfs_ioc_attr_list(
  327. struct xfs_inode *dp,
  328. void __user *ubuf,
  329. size_t bufsize,
  330. int flags,
  331. struct xfs_attrlist_cursor __user *ucursor)
  332. {
  333. struct xfs_attr_list_context context = { };
  334. struct xfs_attrlist *alist;
  335. void *buffer;
  336. int error;
  337. if (bufsize < sizeof(struct xfs_attrlist) ||
  338. bufsize > XFS_XATTR_LIST_MAX)
  339. return -EINVAL;
  340. /*
  341. * Reject flags, only allow namespaces.
  342. */
  343. if (flags & ~(XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
  344. return -EINVAL;
  345. if (flags == (XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
  346. return -EINVAL;
  347. /*
  348. * Validate the cursor.
  349. */
  350. if (copy_from_user(&context.cursor, ucursor, sizeof(context.cursor)))
  351. return -EFAULT;
  352. if (context.cursor.pad1 || context.cursor.pad2)
  353. return -EINVAL;
  354. if (!context.cursor.initted &&
  355. (context.cursor.hashval || context.cursor.blkno ||
  356. context.cursor.offset))
  357. return -EINVAL;
  358. buffer = kvzalloc(bufsize, GFP_KERNEL);
  359. if (!buffer)
  360. return -ENOMEM;
  361. /*
  362. * Initialize the output buffer.
  363. */
  364. context.dp = dp;
  365. context.resynch = 1;
  366. context.attr_filter = xfs_attr_filter(flags);
  367. context.buffer = buffer;
  368. context.bufsize = round_down(bufsize, sizeof(uint32_t));
  369. context.firstu = context.bufsize;
  370. context.put_listent = xfs_ioc_attr_put_listent;
  371. alist = context.buffer;
  372. alist->al_count = 0;
  373. alist->al_more = 0;
  374. alist->al_offset[0] = context.bufsize;
  375. error = xfs_attr_list(&context);
  376. if (error)
  377. goto out_free;
  378. if (copy_to_user(ubuf, buffer, bufsize) ||
  379. copy_to_user(ucursor, &context.cursor, sizeof(context.cursor)))
  380. error = -EFAULT;
  381. out_free:
  382. kmem_free(buffer);
  383. return error;
  384. }
  385. STATIC int
  386. xfs_attrlist_by_handle(
  387. struct file *parfilp,
  388. struct xfs_fsop_attrlist_handlereq __user *p)
  389. {
  390. struct xfs_fsop_attrlist_handlereq al_hreq;
  391. struct dentry *dentry;
  392. int error = -ENOMEM;
  393. if (!capable(CAP_SYS_ADMIN))
  394. return -EPERM;
  395. if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
  396. return -EFAULT;
  397. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  398. if (IS_ERR(dentry))
  399. return PTR_ERR(dentry);
  400. error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)), al_hreq.buffer,
  401. al_hreq.buflen, al_hreq.flags, &p->pos);
  402. dput(dentry);
  403. return error;
  404. }
  405. static int
  406. xfs_attrmulti_attr_get(
  407. struct inode *inode,
  408. unsigned char *name,
  409. unsigned char __user *ubuf,
  410. uint32_t *len,
  411. uint32_t flags)
  412. {
  413. struct xfs_da_args args = {
  414. .dp = XFS_I(inode),
  415. .attr_filter = xfs_attr_filter(flags),
  416. .attr_flags = xfs_attr_flags(flags),
  417. .name = name,
  418. .namelen = strlen(name),
  419. .valuelen = *len,
  420. };
  421. int error;
  422. if (*len > XFS_XATTR_SIZE_MAX)
  423. return -EINVAL;
  424. error = xfs_attr_get(&args);
  425. if (error)
  426. goto out_kfree;
  427. *len = args.valuelen;
  428. if (copy_to_user(ubuf, args.value, args.valuelen))
  429. error = -EFAULT;
  430. out_kfree:
  431. kmem_free(args.value);
  432. return error;
  433. }
  434. static int
  435. xfs_attrmulti_attr_set(
  436. struct inode *inode,
  437. unsigned char *name,
  438. const unsigned char __user *ubuf,
  439. uint32_t len,
  440. uint32_t flags)
  441. {
  442. struct xfs_da_args args = {
  443. .dp = XFS_I(inode),
  444. .attr_filter = xfs_attr_filter(flags),
  445. .attr_flags = xfs_attr_flags(flags),
  446. .name = name,
  447. .namelen = strlen(name),
  448. };
  449. int error;
  450. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  451. return -EPERM;
  452. if (ubuf) {
  453. if (len > XFS_XATTR_SIZE_MAX)
  454. return -EINVAL;
  455. args.value = memdup_user(ubuf, len);
  456. if (IS_ERR(args.value))
  457. return PTR_ERR(args.value);
  458. args.valuelen = len;
  459. }
  460. error = xfs_attr_change(&args);
  461. if (!error && (flags & XFS_IOC_ATTR_ROOT))
  462. xfs_forget_acl(inode, name);
  463. kfree(args.value);
  464. return error;
  465. }
  466. int
  467. xfs_ioc_attrmulti_one(
  468. struct file *parfilp,
  469. struct inode *inode,
  470. uint32_t opcode,
  471. void __user *uname,
  472. void __user *value,
  473. uint32_t *len,
  474. uint32_t flags)
  475. {
  476. unsigned char *name;
  477. int error;
  478. if ((flags & XFS_IOC_ATTR_ROOT) && (flags & XFS_IOC_ATTR_SECURE))
  479. return -EINVAL;
  480. name = strndup_user(uname, MAXNAMELEN);
  481. if (IS_ERR(name))
  482. return PTR_ERR(name);
  483. switch (opcode) {
  484. case ATTR_OP_GET:
  485. error = xfs_attrmulti_attr_get(inode, name, value, len, flags);
  486. break;
  487. case ATTR_OP_REMOVE:
  488. value = NULL;
  489. *len = 0;
  490. fallthrough;
  491. case ATTR_OP_SET:
  492. error = mnt_want_write_file(parfilp);
  493. if (error)
  494. break;
  495. error = xfs_attrmulti_attr_set(inode, name, value, *len, flags);
  496. mnt_drop_write_file(parfilp);
  497. break;
  498. default:
  499. error = -EINVAL;
  500. break;
  501. }
  502. kfree(name);
  503. return error;
  504. }
  505. STATIC int
  506. xfs_attrmulti_by_handle(
  507. struct file *parfilp,
  508. void __user *arg)
  509. {
  510. int error;
  511. xfs_attr_multiop_t *ops;
  512. xfs_fsop_attrmulti_handlereq_t am_hreq;
  513. struct dentry *dentry;
  514. unsigned int i, size;
  515. if (!capable(CAP_SYS_ADMIN))
  516. return -EPERM;
  517. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  518. return -EFAULT;
  519. /* overflow check */
  520. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  521. return -E2BIG;
  522. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  523. if (IS_ERR(dentry))
  524. return PTR_ERR(dentry);
  525. error = -E2BIG;
  526. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  527. if (!size || size > 16 * PAGE_SIZE)
  528. goto out_dput;
  529. ops = memdup_user(am_hreq.ops, size);
  530. if (IS_ERR(ops)) {
  531. error = PTR_ERR(ops);
  532. goto out_dput;
  533. }
  534. error = 0;
  535. for (i = 0; i < am_hreq.opcount; i++) {
  536. ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
  537. d_inode(dentry), ops[i].am_opcode,
  538. ops[i].am_attrname, ops[i].am_attrvalue,
  539. &ops[i].am_length, ops[i].am_flags);
  540. }
  541. if (copy_to_user(am_hreq.ops, ops, size))
  542. error = -EFAULT;
  543. kfree(ops);
  544. out_dput:
  545. dput(dentry);
  546. return error;
  547. }
  548. /* Return 0 on success or positive error */
  549. int
  550. xfs_fsbulkstat_one_fmt(
  551. struct xfs_ibulk *breq,
  552. const struct xfs_bulkstat *bstat)
  553. {
  554. struct xfs_bstat bs1;
  555. xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
  556. if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
  557. return -EFAULT;
  558. return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
  559. }
  560. int
  561. xfs_fsinumbers_fmt(
  562. struct xfs_ibulk *breq,
  563. const struct xfs_inumbers *igrp)
  564. {
  565. struct xfs_inogrp ig1;
  566. xfs_inumbers_to_inogrp(&ig1, igrp);
  567. if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
  568. return -EFAULT;
  569. return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
  570. }
  571. STATIC int
  572. xfs_ioc_fsbulkstat(
  573. struct file *file,
  574. unsigned int cmd,
  575. void __user *arg)
  576. {
  577. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  578. struct xfs_fsop_bulkreq bulkreq;
  579. struct xfs_ibulk breq = {
  580. .mp = mp,
  581. .mnt_userns = file_mnt_user_ns(file),
  582. .ocount = 0,
  583. };
  584. xfs_ino_t lastino;
  585. int error;
  586. /* done = 1 if there are more stats to get and if bulkstat */
  587. /* should be called again (unused here, but used in dmapi) */
  588. if (!capable(CAP_SYS_ADMIN))
  589. return -EPERM;
  590. if (xfs_is_shutdown(mp))
  591. return -EIO;
  592. if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
  593. return -EFAULT;
  594. if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
  595. return -EFAULT;
  596. if (bulkreq.icount <= 0)
  597. return -EINVAL;
  598. if (bulkreq.ubuffer == NULL)
  599. return -EINVAL;
  600. breq.ubuffer = bulkreq.ubuffer;
  601. breq.icount = bulkreq.icount;
  602. /*
  603. * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
  604. * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
  605. * that *lastip contains either zero or the number of the last inode to
  606. * be examined by the previous call and return results starting with
  607. * the next inode after that. The new bulk request back end functions
  608. * take the inode to start with, so we have to compute the startino
  609. * parameter from lastino to maintain correct function. lastino == 0
  610. * is a special case because it has traditionally meant "first inode
  611. * in filesystem".
  612. */
  613. if (cmd == XFS_IOC_FSINUMBERS) {
  614. breq.startino = lastino ? lastino + 1 : 0;
  615. error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
  616. lastino = breq.startino - 1;
  617. } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
  618. breq.startino = lastino;
  619. breq.icount = 1;
  620. error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
  621. } else { /* XFS_IOC_FSBULKSTAT */
  622. breq.startino = lastino ? lastino + 1 : 0;
  623. error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
  624. lastino = breq.startino - 1;
  625. }
  626. if (error)
  627. return error;
  628. if (bulkreq.lastip != NULL &&
  629. copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
  630. return -EFAULT;
  631. if (bulkreq.ocount != NULL &&
  632. copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
  633. return -EFAULT;
  634. return 0;
  635. }
  636. /* Return 0 on success or positive error */
  637. static int
  638. xfs_bulkstat_fmt(
  639. struct xfs_ibulk *breq,
  640. const struct xfs_bulkstat *bstat)
  641. {
  642. if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
  643. return -EFAULT;
  644. return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
  645. }
  646. /*
  647. * Check the incoming bulk request @hdr from userspace and initialize the
  648. * internal @breq bulk request appropriately. Returns 0 if the bulk request
  649. * should proceed; -ECANCELED if there's nothing to do; or the usual
  650. * negative error code.
  651. */
  652. static int
  653. xfs_bulk_ireq_setup(
  654. struct xfs_mount *mp,
  655. struct xfs_bulk_ireq *hdr,
  656. struct xfs_ibulk *breq,
  657. void __user *ubuffer)
  658. {
  659. if (hdr->icount == 0 ||
  660. (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
  661. memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
  662. return -EINVAL;
  663. breq->startino = hdr->ino;
  664. breq->ubuffer = ubuffer;
  665. breq->icount = hdr->icount;
  666. breq->ocount = 0;
  667. breq->flags = 0;
  668. /*
  669. * The @ino parameter is a special value, so we must look it up here.
  670. * We're not allowed to have IREQ_AGNO, and we only return one inode
  671. * worth of data.
  672. */
  673. if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
  674. if (hdr->flags & XFS_BULK_IREQ_AGNO)
  675. return -EINVAL;
  676. switch (hdr->ino) {
  677. case XFS_BULK_IREQ_SPECIAL_ROOT:
  678. hdr->ino = mp->m_sb.sb_rootino;
  679. break;
  680. default:
  681. return -EINVAL;
  682. }
  683. breq->icount = 1;
  684. }
  685. /*
  686. * The IREQ_AGNO flag means that we only want results from a given AG.
  687. * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
  688. * beyond the specified AG then we return no results.
  689. */
  690. if (hdr->flags & XFS_BULK_IREQ_AGNO) {
  691. if (hdr->agno >= mp->m_sb.sb_agcount)
  692. return -EINVAL;
  693. if (breq->startino == 0)
  694. breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
  695. else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
  696. return -EINVAL;
  697. breq->flags |= XFS_IBULK_SAME_AG;
  698. /* Asking for an inode past the end of the AG? We're done! */
  699. if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
  700. return -ECANCELED;
  701. } else if (hdr->agno)
  702. return -EINVAL;
  703. /* Asking for an inode past the end of the FS? We're done! */
  704. if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
  705. return -ECANCELED;
  706. if (hdr->flags & XFS_BULK_IREQ_NREXT64)
  707. breq->flags |= XFS_IBULK_NREXT64;
  708. return 0;
  709. }
  710. /*
  711. * Update the userspace bulk request @hdr to reflect the end state of the
  712. * internal bulk request @breq.
  713. */
  714. static void
  715. xfs_bulk_ireq_teardown(
  716. struct xfs_bulk_ireq *hdr,
  717. struct xfs_ibulk *breq)
  718. {
  719. hdr->ino = breq->startino;
  720. hdr->ocount = breq->ocount;
  721. }
  722. /* Handle the v5 bulkstat ioctl. */
  723. STATIC int
  724. xfs_ioc_bulkstat(
  725. struct file *file,
  726. unsigned int cmd,
  727. struct xfs_bulkstat_req __user *arg)
  728. {
  729. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  730. struct xfs_bulk_ireq hdr;
  731. struct xfs_ibulk breq = {
  732. .mp = mp,
  733. .mnt_userns = file_mnt_user_ns(file),
  734. };
  735. int error;
  736. if (!capable(CAP_SYS_ADMIN))
  737. return -EPERM;
  738. if (xfs_is_shutdown(mp))
  739. return -EIO;
  740. if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
  741. return -EFAULT;
  742. error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
  743. if (error == -ECANCELED)
  744. goto out_teardown;
  745. if (error < 0)
  746. return error;
  747. error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
  748. if (error)
  749. return error;
  750. out_teardown:
  751. xfs_bulk_ireq_teardown(&hdr, &breq);
  752. if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
  753. return -EFAULT;
  754. return 0;
  755. }
  756. STATIC int
  757. xfs_inumbers_fmt(
  758. struct xfs_ibulk *breq,
  759. const struct xfs_inumbers *igrp)
  760. {
  761. if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
  762. return -EFAULT;
  763. return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
  764. }
  765. /* Handle the v5 inumbers ioctl. */
  766. STATIC int
  767. xfs_ioc_inumbers(
  768. struct xfs_mount *mp,
  769. unsigned int cmd,
  770. struct xfs_inumbers_req __user *arg)
  771. {
  772. struct xfs_bulk_ireq hdr;
  773. struct xfs_ibulk breq = {
  774. .mp = mp,
  775. };
  776. int error;
  777. if (!capable(CAP_SYS_ADMIN))
  778. return -EPERM;
  779. if (xfs_is_shutdown(mp))
  780. return -EIO;
  781. if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
  782. return -EFAULT;
  783. error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
  784. if (error == -ECANCELED)
  785. goto out_teardown;
  786. if (error < 0)
  787. return error;
  788. error = xfs_inumbers(&breq, xfs_inumbers_fmt);
  789. if (error)
  790. return error;
  791. out_teardown:
  792. xfs_bulk_ireq_teardown(&hdr, &breq);
  793. if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
  794. return -EFAULT;
  795. return 0;
  796. }
  797. STATIC int
  798. xfs_ioc_fsgeometry(
  799. struct xfs_mount *mp,
  800. void __user *arg,
  801. int struct_version)
  802. {
  803. struct xfs_fsop_geom fsgeo;
  804. size_t len;
  805. xfs_fs_geometry(mp, &fsgeo, struct_version);
  806. if (struct_version <= 3)
  807. len = sizeof(struct xfs_fsop_geom_v1);
  808. else if (struct_version == 4)
  809. len = sizeof(struct xfs_fsop_geom_v4);
  810. else {
  811. xfs_fsop_geom_health(mp, &fsgeo);
  812. len = sizeof(fsgeo);
  813. }
  814. if (copy_to_user(arg, &fsgeo, len))
  815. return -EFAULT;
  816. return 0;
  817. }
  818. STATIC int
  819. xfs_ioc_ag_geometry(
  820. struct xfs_mount *mp,
  821. void __user *arg)
  822. {
  823. struct xfs_perag *pag;
  824. struct xfs_ag_geometry ageo;
  825. int error;
  826. if (copy_from_user(&ageo, arg, sizeof(ageo)))
  827. return -EFAULT;
  828. if (ageo.ag_flags)
  829. return -EINVAL;
  830. if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
  831. return -EINVAL;
  832. pag = xfs_perag_get(mp, ageo.ag_number);
  833. if (!pag)
  834. return -EINVAL;
  835. error = xfs_ag_get_geometry(pag, &ageo);
  836. xfs_perag_put(pag);
  837. if (error)
  838. return error;
  839. if (copy_to_user(arg, &ageo, sizeof(ageo)))
  840. return -EFAULT;
  841. return 0;
  842. }
  843. /*
  844. * Linux extended inode flags interface.
  845. */
  846. static void
  847. xfs_fill_fsxattr(
  848. struct xfs_inode *ip,
  849. int whichfork,
  850. struct fileattr *fa)
  851. {
  852. struct xfs_mount *mp = ip->i_mount;
  853. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  854. fileattr_fill_xflags(fa, xfs_ip2xflags(ip));
  855. if (ip->i_diflags & XFS_DIFLAG_EXTSIZE) {
  856. fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
  857. } else if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
  858. /*
  859. * Don't let a misaligned extent size hint on a directory
  860. * escape to userspace if it won't pass the setattr checks
  861. * later.
  862. */
  863. if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
  864. ip->i_extsize % mp->m_sb.sb_rextsize > 0) {
  865. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |
  866. FS_XFLAG_EXTSZINHERIT);
  867. fa->fsx_extsize = 0;
  868. } else {
  869. fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
  870. }
  871. }
  872. if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
  873. fa->fsx_cowextsize = XFS_FSB_TO_B(mp, ip->i_cowextsize);
  874. fa->fsx_projid = ip->i_projid;
  875. if (ifp && !xfs_need_iread_extents(ifp))
  876. fa->fsx_nextents = xfs_iext_count(ifp);
  877. else
  878. fa->fsx_nextents = xfs_ifork_nextents(ifp);
  879. }
  880. STATIC int
  881. xfs_ioc_fsgetxattra(
  882. xfs_inode_t *ip,
  883. void __user *arg)
  884. {
  885. struct fileattr fa;
  886. xfs_ilock(ip, XFS_ILOCK_SHARED);
  887. xfs_fill_fsxattr(ip, XFS_ATTR_FORK, &fa);
  888. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  889. return copy_fsxattr_to_user(&fa, arg);
  890. }
  891. int
  892. xfs_fileattr_get(
  893. struct dentry *dentry,
  894. struct fileattr *fa)
  895. {
  896. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  897. if (d_is_special(dentry))
  898. return -ENOTTY;
  899. xfs_ilock(ip, XFS_ILOCK_SHARED);
  900. xfs_fill_fsxattr(ip, XFS_DATA_FORK, fa);
  901. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  902. return 0;
  903. }
  904. STATIC uint16_t
  905. xfs_flags2diflags(
  906. struct xfs_inode *ip,
  907. unsigned int xflags)
  908. {
  909. /* can't set PREALLOC this way, just preserve it */
  910. uint16_t di_flags =
  911. (ip->i_diflags & XFS_DIFLAG_PREALLOC);
  912. if (xflags & FS_XFLAG_IMMUTABLE)
  913. di_flags |= XFS_DIFLAG_IMMUTABLE;
  914. if (xflags & FS_XFLAG_APPEND)
  915. di_flags |= XFS_DIFLAG_APPEND;
  916. if (xflags & FS_XFLAG_SYNC)
  917. di_flags |= XFS_DIFLAG_SYNC;
  918. if (xflags & FS_XFLAG_NOATIME)
  919. di_flags |= XFS_DIFLAG_NOATIME;
  920. if (xflags & FS_XFLAG_NODUMP)
  921. di_flags |= XFS_DIFLAG_NODUMP;
  922. if (xflags & FS_XFLAG_NODEFRAG)
  923. di_flags |= XFS_DIFLAG_NODEFRAG;
  924. if (xflags & FS_XFLAG_FILESTREAM)
  925. di_flags |= XFS_DIFLAG_FILESTREAM;
  926. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  927. if (xflags & FS_XFLAG_RTINHERIT)
  928. di_flags |= XFS_DIFLAG_RTINHERIT;
  929. if (xflags & FS_XFLAG_NOSYMLINKS)
  930. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  931. if (xflags & FS_XFLAG_EXTSZINHERIT)
  932. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  933. if (xflags & FS_XFLAG_PROJINHERIT)
  934. di_flags |= XFS_DIFLAG_PROJINHERIT;
  935. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  936. if (xflags & FS_XFLAG_REALTIME)
  937. di_flags |= XFS_DIFLAG_REALTIME;
  938. if (xflags & FS_XFLAG_EXTSIZE)
  939. di_flags |= XFS_DIFLAG_EXTSIZE;
  940. }
  941. return di_flags;
  942. }
  943. STATIC uint64_t
  944. xfs_flags2diflags2(
  945. struct xfs_inode *ip,
  946. unsigned int xflags)
  947. {
  948. uint64_t di_flags2 =
  949. (ip->i_diflags2 & (XFS_DIFLAG2_REFLINK |
  950. XFS_DIFLAG2_BIGTIME |
  951. XFS_DIFLAG2_NREXT64));
  952. if (xflags & FS_XFLAG_DAX)
  953. di_flags2 |= XFS_DIFLAG2_DAX;
  954. if (xflags & FS_XFLAG_COWEXTSIZE)
  955. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  956. return di_flags2;
  957. }
  958. static int
  959. xfs_ioctl_setattr_xflags(
  960. struct xfs_trans *tp,
  961. struct xfs_inode *ip,
  962. struct fileattr *fa)
  963. {
  964. struct xfs_mount *mp = ip->i_mount;
  965. uint64_t i_flags2;
  966. /* Can't change realtime flag if any extents are allocated. */
  967. if ((ip->i_df.if_nextents || ip->i_delayed_blks) &&
  968. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  969. return -EINVAL;
  970. /* If realtime flag is set then must have realtime device */
  971. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  972. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  973. (ip->i_extsize % mp->m_sb.sb_rextsize))
  974. return -EINVAL;
  975. }
  976. /* Clear reflink if we are actually able to set the rt flag. */
  977. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  978. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  979. /* Don't allow us to set DAX mode for a reflinked file for now. */
  980. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  981. return -EINVAL;
  982. /* diflags2 only valid for v3 inodes. */
  983. i_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  984. if (i_flags2 && !xfs_has_v3inodes(mp))
  985. return -EINVAL;
  986. ip->i_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  987. ip->i_diflags2 = i_flags2;
  988. xfs_diflags_to_iflags(ip, false);
  989. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  990. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  991. XFS_STATS_INC(mp, xs_ig_attrchg);
  992. return 0;
  993. }
  994. static void
  995. xfs_ioctl_setattr_prepare_dax(
  996. struct xfs_inode *ip,
  997. struct fileattr *fa)
  998. {
  999. struct xfs_mount *mp = ip->i_mount;
  1000. struct inode *inode = VFS_I(ip);
  1001. if (S_ISDIR(inode->i_mode))
  1002. return;
  1003. if (xfs_has_dax_always(mp) || xfs_has_dax_never(mp))
  1004. return;
  1005. if (((fa->fsx_xflags & FS_XFLAG_DAX) &&
  1006. !(ip->i_diflags2 & XFS_DIFLAG2_DAX)) ||
  1007. (!(fa->fsx_xflags & FS_XFLAG_DAX) &&
  1008. (ip->i_diflags2 & XFS_DIFLAG2_DAX)))
  1009. d_mark_dontcache(inode);
  1010. }
  1011. /*
  1012. * Set up the transaction structure for the setattr operation, checking that we
  1013. * have permission to do so. On success, return a clean transaction and the
  1014. * inode locked exclusively ready for further operation specific checks. On
  1015. * failure, return an error without modifying or locking the inode.
  1016. */
  1017. static struct xfs_trans *
  1018. xfs_ioctl_setattr_get_trans(
  1019. struct xfs_inode *ip,
  1020. struct xfs_dquot *pdqp)
  1021. {
  1022. struct xfs_mount *mp = ip->i_mount;
  1023. struct xfs_trans *tp;
  1024. int error = -EROFS;
  1025. if (xfs_is_readonly(mp))
  1026. goto out_error;
  1027. error = -EIO;
  1028. if (xfs_is_shutdown(mp))
  1029. goto out_error;
  1030. error = xfs_trans_alloc_ichange(ip, NULL, NULL, pdqp,
  1031. has_capability_noaudit(current, CAP_FOWNER), &tp);
  1032. if (error)
  1033. goto out_error;
  1034. if (xfs_has_wsync(mp))
  1035. xfs_trans_set_sync(tp);
  1036. return tp;
  1037. out_error:
  1038. return ERR_PTR(error);
  1039. }
  1040. /*
  1041. * Validate a proposed extent size hint. For regular files, the hint can only
  1042. * be changed if no extents are allocated.
  1043. */
  1044. static int
  1045. xfs_ioctl_setattr_check_extsize(
  1046. struct xfs_inode *ip,
  1047. struct fileattr *fa)
  1048. {
  1049. struct xfs_mount *mp = ip->i_mount;
  1050. xfs_failaddr_t failaddr;
  1051. uint16_t new_diflags;
  1052. if (!fa->fsx_valid)
  1053. return 0;
  1054. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_df.if_nextents &&
  1055. XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
  1056. return -EINVAL;
  1057. if (fa->fsx_extsize & mp->m_blockmask)
  1058. return -EINVAL;
  1059. new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  1060. /*
  1061. * Inode verifiers do not check that the extent size hint is an integer
  1062. * multiple of the rt extent size on a directory with both rtinherit
  1063. * and extszinherit flags set. Don't let sysadmins misconfigure
  1064. * directories.
  1065. */
  1066. if ((new_diflags & XFS_DIFLAG_RTINHERIT) &&
  1067. (new_diflags & XFS_DIFLAG_EXTSZINHERIT)) {
  1068. unsigned int rtextsize_bytes;
  1069. rtextsize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
  1070. if (fa->fsx_extsize % rtextsize_bytes)
  1071. return -EINVAL;
  1072. }
  1073. failaddr = xfs_inode_validate_extsize(ip->i_mount,
  1074. XFS_B_TO_FSB(mp, fa->fsx_extsize),
  1075. VFS_I(ip)->i_mode, new_diflags);
  1076. return failaddr != NULL ? -EINVAL : 0;
  1077. }
  1078. static int
  1079. xfs_ioctl_setattr_check_cowextsize(
  1080. struct xfs_inode *ip,
  1081. struct fileattr *fa)
  1082. {
  1083. struct xfs_mount *mp = ip->i_mount;
  1084. xfs_failaddr_t failaddr;
  1085. uint64_t new_diflags2;
  1086. uint16_t new_diflags;
  1087. if (!fa->fsx_valid)
  1088. return 0;
  1089. if (fa->fsx_cowextsize & mp->m_blockmask)
  1090. return -EINVAL;
  1091. new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  1092. new_diflags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  1093. failaddr = xfs_inode_validate_cowextsize(ip->i_mount,
  1094. XFS_B_TO_FSB(mp, fa->fsx_cowextsize),
  1095. VFS_I(ip)->i_mode, new_diflags, new_diflags2);
  1096. return failaddr != NULL ? -EINVAL : 0;
  1097. }
  1098. static int
  1099. xfs_ioctl_setattr_check_projid(
  1100. struct xfs_inode *ip,
  1101. struct fileattr *fa)
  1102. {
  1103. if (!fa->fsx_valid)
  1104. return 0;
  1105. /* Disallow 32bit project ids if 32bit IDs are not enabled. */
  1106. if (fa->fsx_projid > (uint16_t)-1 &&
  1107. !xfs_has_projid32(ip->i_mount))
  1108. return -EINVAL;
  1109. return 0;
  1110. }
  1111. int
  1112. xfs_fileattr_set(
  1113. struct user_namespace *mnt_userns,
  1114. struct dentry *dentry,
  1115. struct fileattr *fa)
  1116. {
  1117. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  1118. struct xfs_mount *mp = ip->i_mount;
  1119. struct xfs_trans *tp;
  1120. struct xfs_dquot *pdqp = NULL;
  1121. struct xfs_dquot *olddquot = NULL;
  1122. int error;
  1123. trace_xfs_ioctl_setattr(ip);
  1124. if (d_is_special(dentry))
  1125. return -ENOTTY;
  1126. if (!fa->fsx_valid) {
  1127. if (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL |
  1128. FS_NOATIME_FL | FS_NODUMP_FL |
  1129. FS_SYNC_FL | FS_DAX_FL | FS_PROJINHERIT_FL))
  1130. return -EOPNOTSUPP;
  1131. }
  1132. error = xfs_ioctl_setattr_check_projid(ip, fa);
  1133. if (error)
  1134. return error;
  1135. /*
  1136. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1137. * before we start any other transactions. Trying to do this later
  1138. * is messy. We don't care to take a readlock to look at the ids
  1139. * in inode here, because we can't hold it across the trans_reserve.
  1140. * If the IDs do change before we take the ilock, we're covered
  1141. * because the i_*dquot fields will get updated anyway.
  1142. */
  1143. if (fa->fsx_valid && XFS_IS_QUOTA_ON(mp)) {
  1144. error = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
  1145. VFS_I(ip)->i_gid, fa->fsx_projid,
  1146. XFS_QMOPT_PQUOTA, NULL, NULL, &pdqp);
  1147. if (error)
  1148. return error;
  1149. }
  1150. xfs_ioctl_setattr_prepare_dax(ip, fa);
  1151. tp = xfs_ioctl_setattr_get_trans(ip, pdqp);
  1152. if (IS_ERR(tp)) {
  1153. error = PTR_ERR(tp);
  1154. goto error_free_dquots;
  1155. }
  1156. error = xfs_ioctl_setattr_check_extsize(ip, fa);
  1157. if (error)
  1158. goto error_trans_cancel;
  1159. error = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1160. if (error)
  1161. goto error_trans_cancel;
  1162. error = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1163. if (error)
  1164. goto error_trans_cancel;
  1165. if (!fa->fsx_valid)
  1166. goto skip_xattr;
  1167. /*
  1168. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1169. * overrides the following restrictions:
  1170. *
  1171. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1172. * successful return from chown()
  1173. */
  1174. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1175. !capable_wrt_inode_uidgid(mnt_userns, VFS_I(ip), CAP_FSETID))
  1176. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1177. /* Change the ownerships and register project quota modifications */
  1178. if (ip->i_projid != fa->fsx_projid) {
  1179. if (XFS_IS_PQUOTA_ON(mp)) {
  1180. olddquot = xfs_qm_vop_chown(tp, ip,
  1181. &ip->i_pdquot, pdqp);
  1182. }
  1183. ip->i_projid = fa->fsx_projid;
  1184. }
  1185. /*
  1186. * Only set the extent size hint if we've already determined that the
  1187. * extent size hint should be set on the inode. If no extent size flags
  1188. * are set on the inode then unconditionally clear the extent size hint.
  1189. */
  1190. if (ip->i_diflags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1191. ip->i_extsize = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1192. else
  1193. ip->i_extsize = 0;
  1194. if (xfs_has_v3inodes(mp)) {
  1195. if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
  1196. ip->i_cowextsize = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1197. else
  1198. ip->i_cowextsize = 0;
  1199. }
  1200. skip_xattr:
  1201. error = xfs_trans_commit(tp);
  1202. /*
  1203. * Release any dquot(s) the inode had kept before chown.
  1204. */
  1205. xfs_qm_dqrele(olddquot);
  1206. xfs_qm_dqrele(pdqp);
  1207. return error;
  1208. error_trans_cancel:
  1209. xfs_trans_cancel(tp);
  1210. error_free_dquots:
  1211. xfs_qm_dqrele(pdqp);
  1212. return error;
  1213. }
  1214. static bool
  1215. xfs_getbmap_format(
  1216. struct kgetbmap *p,
  1217. struct getbmapx __user *u,
  1218. size_t recsize)
  1219. {
  1220. if (put_user(p->bmv_offset, &u->bmv_offset) ||
  1221. put_user(p->bmv_block, &u->bmv_block) ||
  1222. put_user(p->bmv_length, &u->bmv_length) ||
  1223. put_user(0, &u->bmv_count) ||
  1224. put_user(0, &u->bmv_entries))
  1225. return false;
  1226. if (recsize < sizeof(struct getbmapx))
  1227. return true;
  1228. if (put_user(0, &u->bmv_iflags) ||
  1229. put_user(p->bmv_oflags, &u->bmv_oflags) ||
  1230. put_user(0, &u->bmv_unused1) ||
  1231. put_user(0, &u->bmv_unused2))
  1232. return false;
  1233. return true;
  1234. }
  1235. STATIC int
  1236. xfs_ioc_getbmap(
  1237. struct file *file,
  1238. unsigned int cmd,
  1239. void __user *arg)
  1240. {
  1241. struct getbmapx bmx = { 0 };
  1242. struct kgetbmap *buf;
  1243. size_t recsize;
  1244. int error, i;
  1245. switch (cmd) {
  1246. case XFS_IOC_GETBMAPA:
  1247. bmx.bmv_iflags = BMV_IF_ATTRFORK;
  1248. fallthrough;
  1249. case XFS_IOC_GETBMAP:
  1250. /* struct getbmap is a strict subset of struct getbmapx. */
  1251. recsize = sizeof(struct getbmap);
  1252. break;
  1253. case XFS_IOC_GETBMAPX:
  1254. recsize = sizeof(struct getbmapx);
  1255. break;
  1256. default:
  1257. return -EINVAL;
  1258. }
  1259. if (copy_from_user(&bmx, arg, recsize))
  1260. return -EFAULT;
  1261. if (bmx.bmv_count < 2)
  1262. return -EINVAL;
  1263. if (bmx.bmv_count >= INT_MAX / recsize)
  1264. return -ENOMEM;
  1265. buf = kvcalloc(bmx.bmv_count, sizeof(*buf), GFP_KERNEL);
  1266. if (!buf)
  1267. return -ENOMEM;
  1268. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
  1269. if (error)
  1270. goto out_free_buf;
  1271. error = -EFAULT;
  1272. if (copy_to_user(arg, &bmx, recsize))
  1273. goto out_free_buf;
  1274. arg += recsize;
  1275. for (i = 0; i < bmx.bmv_entries; i++) {
  1276. if (!xfs_getbmap_format(buf + i, arg, recsize))
  1277. goto out_free_buf;
  1278. arg += recsize;
  1279. }
  1280. error = 0;
  1281. out_free_buf:
  1282. kmem_free(buf);
  1283. return error;
  1284. }
  1285. STATIC int
  1286. xfs_ioc_getfsmap(
  1287. struct xfs_inode *ip,
  1288. struct fsmap_head __user *arg)
  1289. {
  1290. struct xfs_fsmap_head xhead = {0};
  1291. struct fsmap_head head;
  1292. struct fsmap *recs;
  1293. unsigned int count;
  1294. __u32 last_flags = 0;
  1295. bool done = false;
  1296. int error;
  1297. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  1298. return -EFAULT;
  1299. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  1300. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  1301. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  1302. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  1303. sizeof(head.fmh_keys[1].fmr_reserved)))
  1304. return -EINVAL;
  1305. /*
  1306. * Use an internal memory buffer so that we don't have to copy fsmap
  1307. * data to userspace while holding locks. Start by trying to allocate
  1308. * up to 128k for the buffer, but fall back to a single page if needed.
  1309. */
  1310. count = min_t(unsigned int, head.fmh_count,
  1311. 131072 / sizeof(struct fsmap));
  1312. recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
  1313. if (!recs) {
  1314. count = min_t(unsigned int, head.fmh_count,
  1315. PAGE_SIZE / sizeof(struct fsmap));
  1316. recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
  1317. if (!recs)
  1318. return -ENOMEM;
  1319. }
  1320. xhead.fmh_iflags = head.fmh_iflags;
  1321. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  1322. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  1323. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1324. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  1325. head.fmh_entries = 0;
  1326. do {
  1327. struct fsmap __user *user_recs;
  1328. struct fsmap *last_rec;
  1329. user_recs = &arg->fmh_recs[head.fmh_entries];
  1330. xhead.fmh_entries = 0;
  1331. xhead.fmh_count = min_t(unsigned int, count,
  1332. head.fmh_count - head.fmh_entries);
  1333. /* Run query, record how many entries we got. */
  1334. error = xfs_getfsmap(ip->i_mount, &xhead, recs);
  1335. switch (error) {
  1336. case 0:
  1337. /*
  1338. * There are no more records in the result set. Copy
  1339. * whatever we got to userspace and break out.
  1340. */
  1341. done = true;
  1342. break;
  1343. case -ECANCELED:
  1344. /*
  1345. * The internal memory buffer is full. Copy whatever
  1346. * records we got to userspace and go again if we have
  1347. * not yet filled the userspace buffer.
  1348. */
  1349. error = 0;
  1350. break;
  1351. default:
  1352. goto out_free;
  1353. }
  1354. head.fmh_entries += xhead.fmh_entries;
  1355. head.fmh_oflags = xhead.fmh_oflags;
  1356. /*
  1357. * If the caller wanted a record count or there aren't any
  1358. * new records to return, we're done.
  1359. */
  1360. if (head.fmh_count == 0 || xhead.fmh_entries == 0)
  1361. break;
  1362. /* Copy all the records we got out to userspace. */
  1363. if (copy_to_user(user_recs, recs,
  1364. xhead.fmh_entries * sizeof(struct fsmap))) {
  1365. error = -EFAULT;
  1366. goto out_free;
  1367. }
  1368. /* Remember the last record flags we copied to userspace. */
  1369. last_rec = &recs[xhead.fmh_entries - 1];
  1370. last_flags = last_rec->fmr_flags;
  1371. /* Set up the low key for the next iteration. */
  1372. xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
  1373. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1374. } while (!done && head.fmh_entries < head.fmh_count);
  1375. /*
  1376. * If there are no more records in the query result set and we're not
  1377. * in counting mode, mark the last record returned with the LAST flag.
  1378. */
  1379. if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
  1380. struct fsmap __user *user_rec;
  1381. last_flags |= FMR_OF_LAST;
  1382. user_rec = &arg->fmh_recs[head.fmh_entries - 1];
  1383. if (copy_to_user(&user_rec->fmr_flags, &last_flags,
  1384. sizeof(last_flags))) {
  1385. error = -EFAULT;
  1386. goto out_free;
  1387. }
  1388. }
  1389. /* copy back header */
  1390. if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
  1391. error = -EFAULT;
  1392. goto out_free;
  1393. }
  1394. out_free:
  1395. kmem_free(recs);
  1396. return error;
  1397. }
  1398. STATIC int
  1399. xfs_ioc_scrub_metadata(
  1400. struct file *file,
  1401. void __user *arg)
  1402. {
  1403. struct xfs_scrub_metadata scrub;
  1404. int error;
  1405. if (!capable(CAP_SYS_ADMIN))
  1406. return -EPERM;
  1407. if (copy_from_user(&scrub, arg, sizeof(scrub)))
  1408. return -EFAULT;
  1409. error = xfs_scrub_metadata(file, &scrub);
  1410. if (error)
  1411. return error;
  1412. if (copy_to_user(arg, &scrub, sizeof(scrub)))
  1413. return -EFAULT;
  1414. return 0;
  1415. }
  1416. int
  1417. xfs_ioc_swapext(
  1418. xfs_swapext_t *sxp)
  1419. {
  1420. xfs_inode_t *ip, *tip;
  1421. struct fd f, tmp;
  1422. int error = 0;
  1423. /* Pull information for the target fd */
  1424. f = fdget((int)sxp->sx_fdtarget);
  1425. if (!f.file) {
  1426. error = -EINVAL;
  1427. goto out;
  1428. }
  1429. if (!(f.file->f_mode & FMODE_WRITE) ||
  1430. !(f.file->f_mode & FMODE_READ) ||
  1431. (f.file->f_flags & O_APPEND)) {
  1432. error = -EBADF;
  1433. goto out_put_file;
  1434. }
  1435. tmp = fdget((int)sxp->sx_fdtmp);
  1436. if (!tmp.file) {
  1437. error = -EINVAL;
  1438. goto out_put_file;
  1439. }
  1440. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1441. !(tmp.file->f_mode & FMODE_READ) ||
  1442. (tmp.file->f_flags & O_APPEND)) {
  1443. error = -EBADF;
  1444. goto out_put_tmp_file;
  1445. }
  1446. if (IS_SWAPFILE(file_inode(f.file)) ||
  1447. IS_SWAPFILE(file_inode(tmp.file))) {
  1448. error = -EINVAL;
  1449. goto out_put_tmp_file;
  1450. }
  1451. /*
  1452. * We need to ensure that the fds passed in point to XFS inodes
  1453. * before we cast and access them as XFS structures as we have no
  1454. * control over what the user passes us here.
  1455. */
  1456. if (f.file->f_op != &xfs_file_operations ||
  1457. tmp.file->f_op != &xfs_file_operations) {
  1458. error = -EINVAL;
  1459. goto out_put_tmp_file;
  1460. }
  1461. ip = XFS_I(file_inode(f.file));
  1462. tip = XFS_I(file_inode(tmp.file));
  1463. if (ip->i_mount != tip->i_mount) {
  1464. error = -EINVAL;
  1465. goto out_put_tmp_file;
  1466. }
  1467. if (ip->i_ino == tip->i_ino) {
  1468. error = -EINVAL;
  1469. goto out_put_tmp_file;
  1470. }
  1471. if (xfs_is_shutdown(ip->i_mount)) {
  1472. error = -EIO;
  1473. goto out_put_tmp_file;
  1474. }
  1475. error = xfs_swap_extents(ip, tip, sxp);
  1476. out_put_tmp_file:
  1477. fdput(tmp);
  1478. out_put_file:
  1479. fdput(f);
  1480. out:
  1481. return error;
  1482. }
  1483. static int
  1484. xfs_ioc_getlabel(
  1485. struct xfs_mount *mp,
  1486. char __user *user_label)
  1487. {
  1488. struct xfs_sb *sbp = &mp->m_sb;
  1489. char label[XFSLABEL_MAX + 1];
  1490. /* Paranoia */
  1491. BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
  1492. /* 1 larger than sb_fname, so this ensures a trailing NUL char */
  1493. memset(label, 0, sizeof(label));
  1494. spin_lock(&mp->m_sb_lock);
  1495. strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
  1496. spin_unlock(&mp->m_sb_lock);
  1497. if (copy_to_user(user_label, label, sizeof(label)))
  1498. return -EFAULT;
  1499. return 0;
  1500. }
  1501. static int
  1502. xfs_ioc_setlabel(
  1503. struct file *filp,
  1504. struct xfs_mount *mp,
  1505. char __user *newlabel)
  1506. {
  1507. struct xfs_sb *sbp = &mp->m_sb;
  1508. char label[XFSLABEL_MAX + 1];
  1509. size_t len;
  1510. int error;
  1511. if (!capable(CAP_SYS_ADMIN))
  1512. return -EPERM;
  1513. /*
  1514. * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
  1515. * smaller, at 12 bytes. We copy one more to be sure we find the
  1516. * (required) NULL character to test the incoming label length.
  1517. * NB: The on disk label doesn't need to be null terminated.
  1518. */
  1519. if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
  1520. return -EFAULT;
  1521. len = strnlen(label, XFSLABEL_MAX + 1);
  1522. if (len > sizeof(sbp->sb_fname))
  1523. return -EINVAL;
  1524. error = mnt_want_write_file(filp);
  1525. if (error)
  1526. return error;
  1527. spin_lock(&mp->m_sb_lock);
  1528. memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
  1529. memcpy(sbp->sb_fname, label, len);
  1530. spin_unlock(&mp->m_sb_lock);
  1531. /*
  1532. * Now we do several things to satisfy userspace.
  1533. * In addition to normal logging of the primary superblock, we also
  1534. * immediately write these changes to sector zero for the primary, then
  1535. * update all backup supers (as xfs_db does for a label change), then
  1536. * invalidate the block device page cache. This is so that any prior
  1537. * buffered reads from userspace (i.e. from blkid) are invalidated,
  1538. * and userspace will see the newly-written label.
  1539. */
  1540. error = xfs_sync_sb_buf(mp);
  1541. if (error)
  1542. goto out;
  1543. /*
  1544. * growfs also updates backup supers so lock against that.
  1545. */
  1546. mutex_lock(&mp->m_growlock);
  1547. error = xfs_update_secondary_sbs(mp);
  1548. mutex_unlock(&mp->m_growlock);
  1549. invalidate_bdev(mp->m_ddev_targp->bt_bdev);
  1550. out:
  1551. mnt_drop_write_file(filp);
  1552. return error;
  1553. }
  1554. static inline int
  1555. xfs_fs_eofblocks_from_user(
  1556. struct xfs_fs_eofblocks *src,
  1557. struct xfs_icwalk *dst)
  1558. {
  1559. if (src->eof_version != XFS_EOFBLOCKS_VERSION)
  1560. return -EINVAL;
  1561. if (src->eof_flags & ~XFS_EOF_FLAGS_VALID)
  1562. return -EINVAL;
  1563. if (memchr_inv(&src->pad32, 0, sizeof(src->pad32)) ||
  1564. memchr_inv(src->pad64, 0, sizeof(src->pad64)))
  1565. return -EINVAL;
  1566. dst->icw_flags = 0;
  1567. if (src->eof_flags & XFS_EOF_FLAGS_SYNC)
  1568. dst->icw_flags |= XFS_ICWALK_FLAG_SYNC;
  1569. if (src->eof_flags & XFS_EOF_FLAGS_UID)
  1570. dst->icw_flags |= XFS_ICWALK_FLAG_UID;
  1571. if (src->eof_flags & XFS_EOF_FLAGS_GID)
  1572. dst->icw_flags |= XFS_ICWALK_FLAG_GID;
  1573. if (src->eof_flags & XFS_EOF_FLAGS_PRID)
  1574. dst->icw_flags |= XFS_ICWALK_FLAG_PRID;
  1575. if (src->eof_flags & XFS_EOF_FLAGS_MINFILESIZE)
  1576. dst->icw_flags |= XFS_ICWALK_FLAG_MINFILESIZE;
  1577. dst->icw_prid = src->eof_prid;
  1578. dst->icw_min_file_size = src->eof_min_file_size;
  1579. dst->icw_uid = INVALID_UID;
  1580. if (src->eof_flags & XFS_EOF_FLAGS_UID) {
  1581. dst->icw_uid = make_kuid(current_user_ns(), src->eof_uid);
  1582. if (!uid_valid(dst->icw_uid))
  1583. return -EINVAL;
  1584. }
  1585. dst->icw_gid = INVALID_GID;
  1586. if (src->eof_flags & XFS_EOF_FLAGS_GID) {
  1587. dst->icw_gid = make_kgid(current_user_ns(), src->eof_gid);
  1588. if (!gid_valid(dst->icw_gid))
  1589. return -EINVAL;
  1590. }
  1591. return 0;
  1592. }
  1593. /*
  1594. * These long-unused ioctls were removed from the official ioctl API in 5.17,
  1595. * but retain these definitions so that we can log warnings about them.
  1596. */
  1597. #define XFS_IOC_ALLOCSP _IOW ('X', 10, struct xfs_flock64)
  1598. #define XFS_IOC_FREESP _IOW ('X', 11, struct xfs_flock64)
  1599. #define XFS_IOC_ALLOCSP64 _IOW ('X', 36, struct xfs_flock64)
  1600. #define XFS_IOC_FREESP64 _IOW ('X', 37, struct xfs_flock64)
  1601. /*
  1602. * Note: some of the ioctl's return positive numbers as a
  1603. * byte count indicating success, such as readlink_by_handle.
  1604. * So we don't "sign flip" like most other routines. This means
  1605. * true errors need to be returned as a negative value.
  1606. */
  1607. long
  1608. xfs_file_ioctl(
  1609. struct file *filp,
  1610. unsigned int cmd,
  1611. unsigned long p)
  1612. {
  1613. struct inode *inode = file_inode(filp);
  1614. struct xfs_inode *ip = XFS_I(inode);
  1615. struct xfs_mount *mp = ip->i_mount;
  1616. void __user *arg = (void __user *)p;
  1617. int error;
  1618. trace_xfs_file_ioctl(ip);
  1619. switch (cmd) {
  1620. case FITRIM:
  1621. return xfs_ioc_trim(mp, arg);
  1622. case FS_IOC_GETFSLABEL:
  1623. return xfs_ioc_getlabel(mp, arg);
  1624. case FS_IOC_SETFSLABEL:
  1625. return xfs_ioc_setlabel(filp, mp, arg);
  1626. case XFS_IOC_ALLOCSP:
  1627. case XFS_IOC_FREESP:
  1628. case XFS_IOC_ALLOCSP64:
  1629. case XFS_IOC_FREESP64:
  1630. xfs_warn_once(mp,
  1631. "%s should use fallocate; XFS_IOC_{ALLOC,FREE}SP ioctl unsupported",
  1632. current->comm);
  1633. return -ENOTTY;
  1634. case XFS_IOC_DIOINFO: {
  1635. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  1636. struct dioattr da;
  1637. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1638. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1639. if (copy_to_user(arg, &da, sizeof(da)))
  1640. return -EFAULT;
  1641. return 0;
  1642. }
  1643. case XFS_IOC_FSBULKSTAT_SINGLE:
  1644. case XFS_IOC_FSBULKSTAT:
  1645. case XFS_IOC_FSINUMBERS:
  1646. return xfs_ioc_fsbulkstat(filp, cmd, arg);
  1647. case XFS_IOC_BULKSTAT:
  1648. return xfs_ioc_bulkstat(filp, cmd, arg);
  1649. case XFS_IOC_INUMBERS:
  1650. return xfs_ioc_inumbers(mp, cmd, arg);
  1651. case XFS_IOC_FSGEOMETRY_V1:
  1652. return xfs_ioc_fsgeometry(mp, arg, 3);
  1653. case XFS_IOC_FSGEOMETRY_V4:
  1654. return xfs_ioc_fsgeometry(mp, arg, 4);
  1655. case XFS_IOC_FSGEOMETRY:
  1656. return xfs_ioc_fsgeometry(mp, arg, 5);
  1657. case XFS_IOC_AG_GEOMETRY:
  1658. return xfs_ioc_ag_geometry(mp, arg);
  1659. case XFS_IOC_GETVERSION:
  1660. return put_user(inode->i_generation, (int __user *)arg);
  1661. case XFS_IOC_FSGETXATTRA:
  1662. return xfs_ioc_fsgetxattra(ip, arg);
  1663. case XFS_IOC_GETBMAP:
  1664. case XFS_IOC_GETBMAPA:
  1665. case XFS_IOC_GETBMAPX:
  1666. return xfs_ioc_getbmap(filp, cmd, arg);
  1667. case FS_IOC_GETFSMAP:
  1668. return xfs_ioc_getfsmap(ip, arg);
  1669. case XFS_IOC_SCRUB_METADATA:
  1670. return xfs_ioc_scrub_metadata(filp, arg);
  1671. case XFS_IOC_FD_TO_HANDLE:
  1672. case XFS_IOC_PATH_TO_HANDLE:
  1673. case XFS_IOC_PATH_TO_FSHANDLE: {
  1674. xfs_fsop_handlereq_t hreq;
  1675. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1676. return -EFAULT;
  1677. return xfs_find_handle(cmd, &hreq);
  1678. }
  1679. case XFS_IOC_OPEN_BY_HANDLE: {
  1680. xfs_fsop_handlereq_t hreq;
  1681. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1682. return -EFAULT;
  1683. return xfs_open_by_handle(filp, &hreq);
  1684. }
  1685. case XFS_IOC_READLINK_BY_HANDLE: {
  1686. xfs_fsop_handlereq_t hreq;
  1687. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1688. return -EFAULT;
  1689. return xfs_readlink_by_handle(filp, &hreq);
  1690. }
  1691. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1692. return xfs_attrlist_by_handle(filp, arg);
  1693. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1694. return xfs_attrmulti_by_handle(filp, arg);
  1695. case XFS_IOC_SWAPEXT: {
  1696. struct xfs_swapext sxp;
  1697. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1698. return -EFAULT;
  1699. error = mnt_want_write_file(filp);
  1700. if (error)
  1701. return error;
  1702. error = xfs_ioc_swapext(&sxp);
  1703. mnt_drop_write_file(filp);
  1704. return error;
  1705. }
  1706. case XFS_IOC_FSCOUNTS: {
  1707. xfs_fsop_counts_t out;
  1708. xfs_fs_counts(mp, &out);
  1709. if (copy_to_user(arg, &out, sizeof(out)))
  1710. return -EFAULT;
  1711. return 0;
  1712. }
  1713. case XFS_IOC_SET_RESBLKS: {
  1714. xfs_fsop_resblks_t inout;
  1715. uint64_t in;
  1716. if (!capable(CAP_SYS_ADMIN))
  1717. return -EPERM;
  1718. if (xfs_is_readonly(mp))
  1719. return -EROFS;
  1720. if (copy_from_user(&inout, arg, sizeof(inout)))
  1721. return -EFAULT;
  1722. error = mnt_want_write_file(filp);
  1723. if (error)
  1724. return error;
  1725. /* input parameter is passed in resblks field of structure */
  1726. in = inout.resblks;
  1727. error = xfs_reserve_blocks(mp, &in, &inout);
  1728. mnt_drop_write_file(filp);
  1729. if (error)
  1730. return error;
  1731. if (copy_to_user(arg, &inout, sizeof(inout)))
  1732. return -EFAULT;
  1733. return 0;
  1734. }
  1735. case XFS_IOC_GET_RESBLKS: {
  1736. xfs_fsop_resblks_t out;
  1737. if (!capable(CAP_SYS_ADMIN))
  1738. return -EPERM;
  1739. error = xfs_reserve_blocks(mp, NULL, &out);
  1740. if (error)
  1741. return error;
  1742. if (copy_to_user(arg, &out, sizeof(out)))
  1743. return -EFAULT;
  1744. return 0;
  1745. }
  1746. case XFS_IOC_FSGROWFSDATA: {
  1747. struct xfs_growfs_data in;
  1748. if (copy_from_user(&in, arg, sizeof(in)))
  1749. return -EFAULT;
  1750. error = mnt_want_write_file(filp);
  1751. if (error)
  1752. return error;
  1753. error = xfs_growfs_data(mp, &in);
  1754. mnt_drop_write_file(filp);
  1755. return error;
  1756. }
  1757. case XFS_IOC_FSGROWFSLOG: {
  1758. struct xfs_growfs_log in;
  1759. if (copy_from_user(&in, arg, sizeof(in)))
  1760. return -EFAULT;
  1761. error = mnt_want_write_file(filp);
  1762. if (error)
  1763. return error;
  1764. error = xfs_growfs_log(mp, &in);
  1765. mnt_drop_write_file(filp);
  1766. return error;
  1767. }
  1768. case XFS_IOC_FSGROWFSRT: {
  1769. xfs_growfs_rt_t in;
  1770. if (copy_from_user(&in, arg, sizeof(in)))
  1771. return -EFAULT;
  1772. error = mnt_want_write_file(filp);
  1773. if (error)
  1774. return error;
  1775. error = xfs_growfs_rt(mp, &in);
  1776. mnt_drop_write_file(filp);
  1777. return error;
  1778. }
  1779. case XFS_IOC_GOINGDOWN: {
  1780. uint32_t in;
  1781. if (!capable(CAP_SYS_ADMIN))
  1782. return -EPERM;
  1783. if (get_user(in, (uint32_t __user *)arg))
  1784. return -EFAULT;
  1785. return xfs_fs_goingdown(mp, in);
  1786. }
  1787. case XFS_IOC_ERROR_INJECTION: {
  1788. xfs_error_injection_t in;
  1789. if (!capable(CAP_SYS_ADMIN))
  1790. return -EPERM;
  1791. if (copy_from_user(&in, arg, sizeof(in)))
  1792. return -EFAULT;
  1793. return xfs_errortag_add(mp, in.errtag);
  1794. }
  1795. case XFS_IOC_ERROR_CLEARALL:
  1796. if (!capable(CAP_SYS_ADMIN))
  1797. return -EPERM;
  1798. return xfs_errortag_clearall(mp);
  1799. case XFS_IOC_FREE_EOFBLOCKS: {
  1800. struct xfs_fs_eofblocks eofb;
  1801. struct xfs_icwalk icw;
  1802. if (!capable(CAP_SYS_ADMIN))
  1803. return -EPERM;
  1804. if (xfs_is_readonly(mp))
  1805. return -EROFS;
  1806. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1807. return -EFAULT;
  1808. error = xfs_fs_eofblocks_from_user(&eofb, &icw);
  1809. if (error)
  1810. return error;
  1811. trace_xfs_ioc_free_eofblocks(mp, &icw, _RET_IP_);
  1812. sb_start_write(mp->m_super);
  1813. error = xfs_blockgc_free_space(mp, &icw);
  1814. sb_end_write(mp->m_super);
  1815. return error;
  1816. }
  1817. default:
  1818. return -ENOTTY;
  1819. }
  1820. }