xfs_ioctl.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  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_ioctl.h"
  15. #include "xfs_alloc.h"
  16. #include "xfs_rtalloc.h"
  17. #include "xfs_itable.h"
  18. #include "xfs_error.h"
  19. #include "xfs_attr.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_bmap_util.h"
  22. #include "xfs_fsops.h"
  23. #include "xfs_discard.h"
  24. #include "xfs_quota.h"
  25. #include "xfs_export.h"
  26. #include "xfs_trace.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_symlink.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 <linux/capability.h>
  37. #include <linux/cred.h>
  38. #include <linux/dcache.h>
  39. #include <linux/mount.h>
  40. #include <linux/namei.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/slab.h>
  43. #include <linux/exportfs.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_lpath((const char __user *)hreq->path, &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. int
  261. xfs_set_dmattrs(
  262. xfs_inode_t *ip,
  263. uint evmask,
  264. uint16_t state)
  265. {
  266. xfs_mount_t *mp = ip->i_mount;
  267. xfs_trans_t *tp;
  268. int error;
  269. if (!capable(CAP_SYS_ADMIN))
  270. return -EPERM;
  271. if (XFS_FORCED_SHUTDOWN(mp))
  272. return -EIO;
  273. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  274. if (error)
  275. return error;
  276. xfs_ilock(ip, XFS_ILOCK_EXCL);
  277. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  278. ip->i_d.di_dmevmask = evmask;
  279. ip->i_d.di_dmstate = state;
  280. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  281. error = xfs_trans_commit(tp);
  282. return error;
  283. }
  284. STATIC int
  285. xfs_fssetdm_by_handle(
  286. struct file *parfilp,
  287. void __user *arg)
  288. {
  289. int error;
  290. struct fsdmidata fsd;
  291. xfs_fsop_setdm_handlereq_t dmhreq;
  292. struct dentry *dentry;
  293. if (!capable(CAP_MKNOD))
  294. return -EPERM;
  295. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  296. return -EFAULT;
  297. error = mnt_want_write_file(parfilp);
  298. if (error)
  299. return error;
  300. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  301. if (IS_ERR(dentry)) {
  302. mnt_drop_write_file(parfilp);
  303. return PTR_ERR(dentry);
  304. }
  305. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  306. error = -EPERM;
  307. goto out;
  308. }
  309. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  310. error = -EFAULT;
  311. goto out;
  312. }
  313. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  314. fsd.fsd_dmstate);
  315. out:
  316. mnt_drop_write_file(parfilp);
  317. dput(dentry);
  318. return error;
  319. }
  320. STATIC int
  321. xfs_attrlist_by_handle(
  322. struct file *parfilp,
  323. void __user *arg)
  324. {
  325. int error = -ENOMEM;
  326. attrlist_cursor_kern_t *cursor;
  327. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  328. xfs_fsop_attrlist_handlereq_t al_hreq;
  329. struct dentry *dentry;
  330. char *kbuf;
  331. if (!capable(CAP_SYS_ADMIN))
  332. return -EPERM;
  333. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  334. return -EFAULT;
  335. if (al_hreq.buflen < sizeof(struct attrlist) ||
  336. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  337. return -EINVAL;
  338. /*
  339. * Reject flags, only allow namespaces.
  340. */
  341. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  342. return -EINVAL;
  343. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  344. if (IS_ERR(dentry))
  345. return PTR_ERR(dentry);
  346. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  347. if (!kbuf)
  348. goto out_dput;
  349. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  350. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  351. al_hreq.flags, cursor);
  352. if (error)
  353. goto out_kfree;
  354. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  355. error = -EFAULT;
  356. goto out_kfree;
  357. }
  358. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  359. error = -EFAULT;
  360. out_kfree:
  361. kmem_free(kbuf);
  362. out_dput:
  363. dput(dentry);
  364. return error;
  365. }
  366. int
  367. xfs_attrmulti_attr_get(
  368. struct inode *inode,
  369. unsigned char *name,
  370. unsigned char __user *ubuf,
  371. uint32_t *len,
  372. uint32_t flags)
  373. {
  374. unsigned char *kbuf;
  375. int error = -EFAULT;
  376. if (*len > XFS_XATTR_SIZE_MAX)
  377. return -EINVAL;
  378. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  379. if (!kbuf)
  380. return -ENOMEM;
  381. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  382. if (error)
  383. goto out_kfree;
  384. if (copy_to_user(ubuf, kbuf, *len))
  385. error = -EFAULT;
  386. out_kfree:
  387. kmem_free(kbuf);
  388. return error;
  389. }
  390. int
  391. xfs_attrmulti_attr_set(
  392. struct inode *inode,
  393. unsigned char *name,
  394. const unsigned char __user *ubuf,
  395. uint32_t len,
  396. uint32_t flags)
  397. {
  398. unsigned char *kbuf;
  399. int error;
  400. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  401. return -EPERM;
  402. if (len > XFS_XATTR_SIZE_MAX)
  403. return -EINVAL;
  404. kbuf = memdup_user(ubuf, len);
  405. if (IS_ERR(kbuf))
  406. return PTR_ERR(kbuf);
  407. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  408. if (!error)
  409. xfs_forget_acl(inode, name, flags);
  410. kfree(kbuf);
  411. return error;
  412. }
  413. int
  414. xfs_attrmulti_attr_remove(
  415. struct inode *inode,
  416. unsigned char *name,
  417. uint32_t flags)
  418. {
  419. int error;
  420. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  421. return -EPERM;
  422. error = xfs_attr_remove(XFS_I(inode), name, flags);
  423. if (!error)
  424. xfs_forget_acl(inode, name, flags);
  425. return error;
  426. }
  427. STATIC int
  428. xfs_attrmulti_by_handle(
  429. struct file *parfilp,
  430. void __user *arg)
  431. {
  432. int error;
  433. xfs_attr_multiop_t *ops;
  434. xfs_fsop_attrmulti_handlereq_t am_hreq;
  435. struct dentry *dentry;
  436. unsigned int i, size;
  437. unsigned char *attr_name;
  438. if (!capable(CAP_SYS_ADMIN))
  439. return -EPERM;
  440. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  441. return -EFAULT;
  442. /* overflow check */
  443. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  444. return -E2BIG;
  445. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  446. if (IS_ERR(dentry))
  447. return PTR_ERR(dentry);
  448. error = -E2BIG;
  449. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  450. if (!size || size > 16 * PAGE_SIZE)
  451. goto out_dput;
  452. ops = memdup_user(am_hreq.ops, size);
  453. if (IS_ERR(ops)) {
  454. error = PTR_ERR(ops);
  455. goto out_dput;
  456. }
  457. error = -ENOMEM;
  458. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  459. if (!attr_name)
  460. goto out_kfree_ops;
  461. error = 0;
  462. for (i = 0; i < am_hreq.opcount; i++) {
  463. ops[i].am_error = strncpy_from_user((char *)attr_name,
  464. ops[i].am_attrname, MAXNAMELEN);
  465. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  466. error = -ERANGE;
  467. if (ops[i].am_error < 0)
  468. break;
  469. switch (ops[i].am_opcode) {
  470. case ATTR_OP_GET:
  471. ops[i].am_error = xfs_attrmulti_attr_get(
  472. d_inode(dentry), attr_name,
  473. ops[i].am_attrvalue, &ops[i].am_length,
  474. ops[i].am_flags);
  475. break;
  476. case ATTR_OP_SET:
  477. ops[i].am_error = mnt_want_write_file(parfilp);
  478. if (ops[i].am_error)
  479. break;
  480. ops[i].am_error = xfs_attrmulti_attr_set(
  481. d_inode(dentry), attr_name,
  482. ops[i].am_attrvalue, ops[i].am_length,
  483. ops[i].am_flags);
  484. mnt_drop_write_file(parfilp);
  485. break;
  486. case ATTR_OP_REMOVE:
  487. ops[i].am_error = mnt_want_write_file(parfilp);
  488. if (ops[i].am_error)
  489. break;
  490. ops[i].am_error = xfs_attrmulti_attr_remove(
  491. d_inode(dentry), attr_name,
  492. ops[i].am_flags);
  493. mnt_drop_write_file(parfilp);
  494. break;
  495. default:
  496. ops[i].am_error = -EINVAL;
  497. }
  498. }
  499. if (copy_to_user(am_hreq.ops, ops, size))
  500. error = -EFAULT;
  501. kfree(attr_name);
  502. out_kfree_ops:
  503. kfree(ops);
  504. out_dput:
  505. dput(dentry);
  506. return error;
  507. }
  508. int
  509. xfs_ioc_space(
  510. struct file *filp,
  511. unsigned int cmd,
  512. xfs_flock64_t *bf)
  513. {
  514. struct inode *inode = file_inode(filp);
  515. struct xfs_inode *ip = XFS_I(inode);
  516. struct iattr iattr;
  517. enum xfs_prealloc_flags flags = 0;
  518. uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  519. int error;
  520. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  521. return -EPERM;
  522. if (!(filp->f_mode & FMODE_WRITE))
  523. return -EBADF;
  524. if (!S_ISREG(inode->i_mode))
  525. return -EINVAL;
  526. if (filp->f_flags & O_DSYNC)
  527. flags |= XFS_PREALLOC_SYNC;
  528. if (filp->f_mode & FMODE_NOCMTIME)
  529. flags |= XFS_PREALLOC_INVISIBLE;
  530. error = mnt_want_write_file(filp);
  531. if (error)
  532. return error;
  533. xfs_ilock(ip, iolock);
  534. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  535. if (error)
  536. goto out_unlock;
  537. switch (bf->l_whence) {
  538. case 0: /*SEEK_SET*/
  539. break;
  540. case 1: /*SEEK_CUR*/
  541. bf->l_start += filp->f_pos;
  542. break;
  543. case 2: /*SEEK_END*/
  544. bf->l_start += XFS_ISIZE(ip);
  545. break;
  546. default:
  547. error = -EINVAL;
  548. goto out_unlock;
  549. }
  550. /*
  551. * length of <= 0 for resv/unresv/zero is invalid. length for
  552. * alloc/free is ignored completely and we have no idea what userspace
  553. * might have set it to, so set it to zero to allow range
  554. * checks to pass.
  555. */
  556. switch (cmd) {
  557. case XFS_IOC_ZERO_RANGE:
  558. case XFS_IOC_RESVSP:
  559. case XFS_IOC_RESVSP64:
  560. case XFS_IOC_UNRESVSP:
  561. case XFS_IOC_UNRESVSP64:
  562. if (bf->l_len <= 0) {
  563. error = -EINVAL;
  564. goto out_unlock;
  565. }
  566. break;
  567. default:
  568. bf->l_len = 0;
  569. break;
  570. }
  571. if (bf->l_start < 0 ||
  572. bf->l_start > inode->i_sb->s_maxbytes ||
  573. bf->l_start + bf->l_len < 0 ||
  574. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  575. error = -EINVAL;
  576. goto out_unlock;
  577. }
  578. switch (cmd) {
  579. case XFS_IOC_ZERO_RANGE:
  580. flags |= XFS_PREALLOC_SET;
  581. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  582. break;
  583. case XFS_IOC_RESVSP:
  584. case XFS_IOC_RESVSP64:
  585. flags |= XFS_PREALLOC_SET;
  586. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  587. XFS_BMAPI_PREALLOC);
  588. break;
  589. case XFS_IOC_UNRESVSP:
  590. case XFS_IOC_UNRESVSP64:
  591. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  592. break;
  593. case XFS_IOC_ALLOCSP:
  594. case XFS_IOC_ALLOCSP64:
  595. case XFS_IOC_FREESP:
  596. case XFS_IOC_FREESP64:
  597. flags |= XFS_PREALLOC_CLEAR;
  598. if (bf->l_start > XFS_ISIZE(ip)) {
  599. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  600. bf->l_start - XFS_ISIZE(ip), 0);
  601. if (error)
  602. goto out_unlock;
  603. }
  604. iattr.ia_valid = ATTR_SIZE;
  605. iattr.ia_size = bf->l_start;
  606. error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
  607. break;
  608. default:
  609. ASSERT(0);
  610. error = -EINVAL;
  611. }
  612. if (error)
  613. goto out_unlock;
  614. error = xfs_update_prealloc_flags(ip, flags);
  615. out_unlock:
  616. xfs_iunlock(ip, iolock);
  617. mnt_drop_write_file(filp);
  618. return error;
  619. }
  620. STATIC int
  621. xfs_ioc_bulkstat(
  622. xfs_mount_t *mp,
  623. unsigned int cmd,
  624. void __user *arg)
  625. {
  626. xfs_fsop_bulkreq_t bulkreq;
  627. int count; /* # of records returned */
  628. xfs_ino_t inlast; /* last inode number */
  629. int done;
  630. int error;
  631. /* done = 1 if there are more stats to get and if bulkstat */
  632. /* should be called again (unused here, but used in dmapi) */
  633. if (!capable(CAP_SYS_ADMIN))
  634. return -EPERM;
  635. if (XFS_FORCED_SHUTDOWN(mp))
  636. return -EIO;
  637. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  638. return -EFAULT;
  639. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  640. return -EFAULT;
  641. if ((count = bulkreq.icount) <= 0)
  642. return -EINVAL;
  643. if (bulkreq.ubuffer == NULL)
  644. return -EINVAL;
  645. if (cmd == XFS_IOC_FSINUMBERS)
  646. error = xfs_inumbers(mp, &inlast, &count,
  647. bulkreq.ubuffer, xfs_inumbers_fmt);
  648. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  649. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  650. sizeof(xfs_bstat_t), NULL, &done);
  651. else /* XFS_IOC_FSBULKSTAT */
  652. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  653. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  654. &done);
  655. if (error)
  656. return error;
  657. if (bulkreq.ocount != NULL) {
  658. if (copy_to_user(bulkreq.lastip, &inlast,
  659. sizeof(xfs_ino_t)))
  660. return -EFAULT;
  661. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  662. return -EFAULT;
  663. }
  664. return 0;
  665. }
  666. STATIC int
  667. xfs_ioc_fsgeometry_v1(
  668. xfs_mount_t *mp,
  669. void __user *arg)
  670. {
  671. xfs_fsop_geom_t fsgeo;
  672. int error;
  673. error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
  674. if (error)
  675. return error;
  676. /*
  677. * Caller should have passed an argument of type
  678. * xfs_fsop_geom_v1_t. This is a proper subset of the
  679. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  680. */
  681. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  682. return -EFAULT;
  683. return 0;
  684. }
  685. STATIC int
  686. xfs_ioc_fsgeometry(
  687. xfs_mount_t *mp,
  688. void __user *arg)
  689. {
  690. xfs_fsop_geom_t fsgeo;
  691. int error;
  692. error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 4);
  693. if (error)
  694. return error;
  695. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  696. return -EFAULT;
  697. return 0;
  698. }
  699. /*
  700. * Linux extended inode flags interface.
  701. */
  702. STATIC unsigned int
  703. xfs_merge_ioc_xflags(
  704. unsigned int flags,
  705. unsigned int start)
  706. {
  707. unsigned int xflags = start;
  708. if (flags & FS_IMMUTABLE_FL)
  709. xflags |= FS_XFLAG_IMMUTABLE;
  710. else
  711. xflags &= ~FS_XFLAG_IMMUTABLE;
  712. if (flags & FS_APPEND_FL)
  713. xflags |= FS_XFLAG_APPEND;
  714. else
  715. xflags &= ~FS_XFLAG_APPEND;
  716. if (flags & FS_SYNC_FL)
  717. xflags |= FS_XFLAG_SYNC;
  718. else
  719. xflags &= ~FS_XFLAG_SYNC;
  720. if (flags & FS_NOATIME_FL)
  721. xflags |= FS_XFLAG_NOATIME;
  722. else
  723. xflags &= ~FS_XFLAG_NOATIME;
  724. if (flags & FS_NODUMP_FL)
  725. xflags |= FS_XFLAG_NODUMP;
  726. else
  727. xflags &= ~FS_XFLAG_NODUMP;
  728. return xflags;
  729. }
  730. STATIC unsigned int
  731. xfs_di2lxflags(
  732. uint16_t di_flags)
  733. {
  734. unsigned int flags = 0;
  735. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  736. flags |= FS_IMMUTABLE_FL;
  737. if (di_flags & XFS_DIFLAG_APPEND)
  738. flags |= FS_APPEND_FL;
  739. if (di_flags & XFS_DIFLAG_SYNC)
  740. flags |= FS_SYNC_FL;
  741. if (di_flags & XFS_DIFLAG_NOATIME)
  742. flags |= FS_NOATIME_FL;
  743. if (di_flags & XFS_DIFLAG_NODUMP)
  744. flags |= FS_NODUMP_FL;
  745. return flags;
  746. }
  747. STATIC int
  748. xfs_ioc_fsgetxattr(
  749. xfs_inode_t *ip,
  750. int attr,
  751. void __user *arg)
  752. {
  753. struct fsxattr fa;
  754. memset(&fa, 0, sizeof(struct fsxattr));
  755. xfs_ilock(ip, XFS_ILOCK_SHARED);
  756. fa.fsx_xflags = xfs_ip2xflags(ip);
  757. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  758. fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
  759. ip->i_mount->m_sb.sb_blocklog;
  760. fa.fsx_projid = xfs_get_projid(ip);
  761. if (attr) {
  762. if (ip->i_afp) {
  763. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  764. fa.fsx_nextents = xfs_iext_count(ip->i_afp);
  765. else
  766. fa.fsx_nextents = ip->i_d.di_anextents;
  767. } else
  768. fa.fsx_nextents = 0;
  769. } else {
  770. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  771. fa.fsx_nextents = xfs_iext_count(&ip->i_df);
  772. else
  773. fa.fsx_nextents = ip->i_d.di_nextents;
  774. }
  775. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  776. if (copy_to_user(arg, &fa, sizeof(fa)))
  777. return -EFAULT;
  778. return 0;
  779. }
  780. STATIC uint16_t
  781. xfs_flags2diflags(
  782. struct xfs_inode *ip,
  783. unsigned int xflags)
  784. {
  785. /* can't set PREALLOC this way, just preserve it */
  786. uint16_t di_flags =
  787. (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  788. if (xflags & FS_XFLAG_IMMUTABLE)
  789. di_flags |= XFS_DIFLAG_IMMUTABLE;
  790. if (xflags & FS_XFLAG_APPEND)
  791. di_flags |= XFS_DIFLAG_APPEND;
  792. if (xflags & FS_XFLAG_SYNC)
  793. di_flags |= XFS_DIFLAG_SYNC;
  794. if (xflags & FS_XFLAG_NOATIME)
  795. di_flags |= XFS_DIFLAG_NOATIME;
  796. if (xflags & FS_XFLAG_NODUMP)
  797. di_flags |= XFS_DIFLAG_NODUMP;
  798. if (xflags & FS_XFLAG_NODEFRAG)
  799. di_flags |= XFS_DIFLAG_NODEFRAG;
  800. if (xflags & FS_XFLAG_FILESTREAM)
  801. di_flags |= XFS_DIFLAG_FILESTREAM;
  802. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  803. if (xflags & FS_XFLAG_RTINHERIT)
  804. di_flags |= XFS_DIFLAG_RTINHERIT;
  805. if (xflags & FS_XFLAG_NOSYMLINKS)
  806. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  807. if (xflags & FS_XFLAG_EXTSZINHERIT)
  808. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  809. if (xflags & FS_XFLAG_PROJINHERIT)
  810. di_flags |= XFS_DIFLAG_PROJINHERIT;
  811. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  812. if (xflags & FS_XFLAG_REALTIME)
  813. di_flags |= XFS_DIFLAG_REALTIME;
  814. if (xflags & FS_XFLAG_EXTSIZE)
  815. di_flags |= XFS_DIFLAG_EXTSIZE;
  816. }
  817. return di_flags;
  818. }
  819. STATIC uint64_t
  820. xfs_flags2diflags2(
  821. struct xfs_inode *ip,
  822. unsigned int xflags)
  823. {
  824. uint64_t di_flags2 =
  825. (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
  826. if (xflags & FS_XFLAG_DAX)
  827. di_flags2 |= XFS_DIFLAG2_DAX;
  828. if (xflags & FS_XFLAG_COWEXTSIZE)
  829. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  830. return di_flags2;
  831. }
  832. STATIC void
  833. xfs_diflags_to_linux(
  834. struct xfs_inode *ip)
  835. {
  836. struct inode *inode = VFS_I(ip);
  837. unsigned int xflags = xfs_ip2xflags(ip);
  838. if (xflags & FS_XFLAG_IMMUTABLE)
  839. inode->i_flags |= S_IMMUTABLE;
  840. else
  841. inode->i_flags &= ~S_IMMUTABLE;
  842. if (xflags & FS_XFLAG_APPEND)
  843. inode->i_flags |= S_APPEND;
  844. else
  845. inode->i_flags &= ~S_APPEND;
  846. if (xflags & FS_XFLAG_SYNC)
  847. inode->i_flags |= S_SYNC;
  848. else
  849. inode->i_flags &= ~S_SYNC;
  850. if (xflags & FS_XFLAG_NOATIME)
  851. inode->i_flags |= S_NOATIME;
  852. else
  853. inode->i_flags &= ~S_NOATIME;
  854. #if 0 /* disabled until the flag switching races are sorted out */
  855. if (xflags & FS_XFLAG_DAX)
  856. inode->i_flags |= S_DAX;
  857. else
  858. inode->i_flags &= ~S_DAX;
  859. #endif
  860. }
  861. static int
  862. xfs_ioctl_setattr_xflags(
  863. struct xfs_trans *tp,
  864. struct xfs_inode *ip,
  865. struct fsxattr *fa)
  866. {
  867. struct xfs_mount *mp = ip->i_mount;
  868. uint64_t di_flags2;
  869. /* Can't change realtime flag if any extents are allocated. */
  870. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  871. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  872. return -EINVAL;
  873. /* If realtime flag is set then must have realtime device */
  874. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  875. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  876. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  877. return -EINVAL;
  878. }
  879. /* Clear reflink if we are actually able to set the rt flag. */
  880. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  881. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  882. /* Don't allow us to set DAX mode for a reflinked file for now. */
  883. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  884. return -EINVAL;
  885. /*
  886. * Can't modify an immutable/append-only file unless
  887. * we have appropriate permission.
  888. */
  889. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  890. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  891. !capable(CAP_LINUX_IMMUTABLE))
  892. return -EPERM;
  893. /* diflags2 only valid for v3 inodes. */
  894. di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  895. if (di_flags2 && ip->i_d.di_version < 3)
  896. return -EINVAL;
  897. ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
  898. ip->i_d.di_flags2 = di_flags2;
  899. xfs_diflags_to_linux(ip);
  900. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  901. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  902. XFS_STATS_INC(mp, xs_ig_attrchg);
  903. return 0;
  904. }
  905. /*
  906. * If we are changing DAX flags, we have to ensure the file is clean and any
  907. * cached objects in the address space are invalidated and removed. This
  908. * requires us to lock out other IO and page faults similar to a truncate
  909. * operation. The locks need to be held until the transaction has been committed
  910. * so that the cache invalidation is atomic with respect to the DAX flag
  911. * manipulation.
  912. */
  913. static int
  914. xfs_ioctl_setattr_dax_invalidate(
  915. struct xfs_inode *ip,
  916. struct fsxattr *fa,
  917. int *join_flags)
  918. {
  919. struct inode *inode = VFS_I(ip);
  920. struct super_block *sb = inode->i_sb;
  921. int error;
  922. *join_flags = 0;
  923. /*
  924. * It is only valid to set the DAX flag on regular files and
  925. * directories on filesystems where the block size is equal to the page
  926. * size. On directories it serves as an inherited hint so we don't
  927. * have to check the device for dax support or flush pagecache.
  928. */
  929. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  930. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  931. return -EINVAL;
  932. if (S_ISREG(inode->i_mode) &&
  933. !bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
  934. sb->s_blocksize))
  935. return -EINVAL;
  936. }
  937. /* If the DAX state is not changing, we have nothing to do here. */
  938. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  939. return 0;
  940. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  941. return 0;
  942. if (S_ISDIR(inode->i_mode))
  943. return 0;
  944. /* lock, flush and invalidate mapping in preparation for flag change */
  945. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  946. error = filemap_write_and_wait(inode->i_mapping);
  947. if (error)
  948. goto out_unlock;
  949. error = invalidate_inode_pages2(inode->i_mapping);
  950. if (error)
  951. goto out_unlock;
  952. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  953. return 0;
  954. out_unlock:
  955. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  956. return error;
  957. }
  958. /*
  959. * Set up the transaction structure for the setattr operation, checking that we
  960. * have permission to do so. On success, return a clean transaction and the
  961. * inode locked exclusively ready for further operation specific checks. On
  962. * failure, return an error without modifying or locking the inode.
  963. *
  964. * The inode might already be IO locked on call. If this is the case, it is
  965. * indicated in @join_flags and we take full responsibility for ensuring they
  966. * are unlocked from now on. Hence if we have an error here, we still have to
  967. * unlock them. Otherwise, once they are joined to the transaction, they will
  968. * be unlocked on commit/cancel.
  969. */
  970. static struct xfs_trans *
  971. xfs_ioctl_setattr_get_trans(
  972. struct xfs_inode *ip,
  973. int join_flags)
  974. {
  975. struct xfs_mount *mp = ip->i_mount;
  976. struct xfs_trans *tp;
  977. int error = -EROFS;
  978. if (mp->m_flags & XFS_MOUNT_RDONLY)
  979. goto out_unlock;
  980. error = -EIO;
  981. if (XFS_FORCED_SHUTDOWN(mp))
  982. goto out_unlock;
  983. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  984. if (error)
  985. return ERR_PTR(error);
  986. xfs_ilock(ip, XFS_ILOCK_EXCL);
  987. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  988. join_flags = 0;
  989. /*
  990. * CAP_FOWNER overrides the following restrictions:
  991. *
  992. * The user ID of the calling process must be equal to the file owner
  993. * ID, except in cases where the CAP_FSETID capability is applicable.
  994. */
  995. if (!inode_owner_or_capable(VFS_I(ip))) {
  996. error = -EPERM;
  997. goto out_cancel;
  998. }
  999. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1000. xfs_trans_set_sync(tp);
  1001. return tp;
  1002. out_cancel:
  1003. xfs_trans_cancel(tp);
  1004. out_unlock:
  1005. if (join_flags)
  1006. xfs_iunlock(ip, join_flags);
  1007. return ERR_PTR(error);
  1008. }
  1009. /*
  1010. * extent size hint validation is somewhat cumbersome. Rules are:
  1011. *
  1012. * 1. extent size hint is only valid for directories and regular files
  1013. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1014. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1015. * 4. can only be changed on regular files if no extents are allocated
  1016. * 5. can be changed on directories at any time
  1017. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1018. * 7. Extent size must be a multiple of the appropriate block size.
  1019. * 8. for non-realtime files, the extent size hint must be limited
  1020. * to half the AG size to avoid alignment extending the extent beyond the
  1021. * limits of the AG.
  1022. *
  1023. * Please keep this function in sync with xfs_scrub_inode_extsize.
  1024. */
  1025. static int
  1026. xfs_ioctl_setattr_check_extsize(
  1027. struct xfs_inode *ip,
  1028. struct fsxattr *fa)
  1029. {
  1030. struct xfs_mount *mp = ip->i_mount;
  1031. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1032. return -EINVAL;
  1033. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1034. !S_ISDIR(VFS_I(ip)->i_mode))
  1035. return -EINVAL;
  1036. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1037. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1038. return -EINVAL;
  1039. if (fa->fsx_extsize != 0) {
  1040. xfs_extlen_t size;
  1041. xfs_fsblock_t extsize_fsb;
  1042. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1043. if (extsize_fsb > MAXEXTLEN)
  1044. return -EINVAL;
  1045. if (XFS_IS_REALTIME_INODE(ip) ||
  1046. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1047. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1048. } else {
  1049. size = mp->m_sb.sb_blocksize;
  1050. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1051. return -EINVAL;
  1052. }
  1053. if (fa->fsx_extsize % size)
  1054. return -EINVAL;
  1055. } else
  1056. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1057. return 0;
  1058. }
  1059. /*
  1060. * CoW extent size hint validation rules are:
  1061. *
  1062. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  1063. * The inode does not have to have any shared blocks, but it must be a v3.
  1064. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  1065. * for a directory, the hint is propagated to new files.
  1066. * 3. Can be changed on files & directories at any time.
  1067. * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
  1068. * 5. Extent size must be a multiple of the appropriate block size.
  1069. * 6. The extent size hint must be limited to half the AG size to avoid
  1070. * alignment extending the extent beyond the limits of the AG.
  1071. *
  1072. * Please keep this function in sync with xfs_scrub_inode_cowextsize.
  1073. */
  1074. static int
  1075. xfs_ioctl_setattr_check_cowextsize(
  1076. struct xfs_inode *ip,
  1077. struct fsxattr *fa)
  1078. {
  1079. struct xfs_mount *mp = ip->i_mount;
  1080. if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
  1081. return 0;
  1082. if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
  1083. ip->i_d.di_version != 3)
  1084. return -EINVAL;
  1085. if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
  1086. return -EINVAL;
  1087. if (fa->fsx_cowextsize != 0) {
  1088. xfs_extlen_t size;
  1089. xfs_fsblock_t cowextsize_fsb;
  1090. cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1091. if (cowextsize_fsb > MAXEXTLEN)
  1092. return -EINVAL;
  1093. size = mp->m_sb.sb_blocksize;
  1094. if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
  1095. return -EINVAL;
  1096. if (fa->fsx_cowextsize % size)
  1097. return -EINVAL;
  1098. } else
  1099. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  1100. return 0;
  1101. }
  1102. static int
  1103. xfs_ioctl_setattr_check_projid(
  1104. struct xfs_inode *ip,
  1105. struct fsxattr *fa)
  1106. {
  1107. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1108. if (fa->fsx_projid > (uint16_t)-1 &&
  1109. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1110. return -EINVAL;
  1111. /*
  1112. * Project Quota ID state is only allowed to change from within the init
  1113. * namespace. Enforce that restriction only if we are trying to change
  1114. * the quota ID state. Everything else is allowed in user namespaces.
  1115. */
  1116. if (current_user_ns() == &init_user_ns)
  1117. return 0;
  1118. if (xfs_get_projid(ip) != fa->fsx_projid)
  1119. return -EINVAL;
  1120. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1121. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1122. return -EINVAL;
  1123. return 0;
  1124. }
  1125. STATIC int
  1126. xfs_ioctl_setattr(
  1127. xfs_inode_t *ip,
  1128. struct fsxattr *fa)
  1129. {
  1130. struct xfs_mount *mp = ip->i_mount;
  1131. struct xfs_trans *tp;
  1132. struct xfs_dquot *udqp = NULL;
  1133. struct xfs_dquot *pdqp = NULL;
  1134. struct xfs_dquot *olddquot = NULL;
  1135. int code;
  1136. int join_flags = 0;
  1137. trace_xfs_ioctl_setattr(ip);
  1138. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1139. if (code)
  1140. return code;
  1141. /*
  1142. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1143. * before we start any other transactions. Trying to do this later
  1144. * is messy. We don't care to take a readlock to look at the ids
  1145. * in inode here, because we can't hold it across the trans_reserve.
  1146. * If the IDs do change before we take the ilock, we're covered
  1147. * because the i_*dquot fields will get updated anyway.
  1148. */
  1149. if (XFS_IS_QUOTA_ON(mp)) {
  1150. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1151. ip->i_d.di_gid, fa->fsx_projid,
  1152. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1153. if (code)
  1154. return code;
  1155. }
  1156. /*
  1157. * Changing DAX config may require inode locking for mapping
  1158. * invalidation. These need to be held all the way to transaction commit
  1159. * or cancel time, so need to be passed through to
  1160. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1161. * appropriately.
  1162. */
  1163. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1164. if (code)
  1165. goto error_free_dquots;
  1166. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1167. if (IS_ERR(tp)) {
  1168. code = PTR_ERR(tp);
  1169. goto error_free_dquots;
  1170. }
  1171. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1172. xfs_get_projid(ip) != fa->fsx_projid) {
  1173. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1174. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1175. if (code) /* out of quota */
  1176. goto error_trans_cancel;
  1177. }
  1178. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1179. if (code)
  1180. goto error_trans_cancel;
  1181. code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1182. if (code)
  1183. goto error_trans_cancel;
  1184. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1185. if (code)
  1186. goto error_trans_cancel;
  1187. /*
  1188. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1189. * overrides the following restrictions:
  1190. *
  1191. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1192. * successful return from chown()
  1193. */
  1194. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1195. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1196. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1197. /* Change the ownerships and register project quota modifications */
  1198. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1199. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1200. olddquot = xfs_qm_vop_chown(tp, ip,
  1201. &ip->i_pdquot, pdqp);
  1202. }
  1203. ASSERT(ip->i_d.di_version > 1);
  1204. xfs_set_projid(ip, fa->fsx_projid);
  1205. }
  1206. /*
  1207. * Only set the extent size hint if we've already determined that the
  1208. * extent size hint should be set on the inode. If no extent size flags
  1209. * are set on the inode then unconditionally clear the extent size hint.
  1210. */
  1211. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1212. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1213. else
  1214. ip->i_d.di_extsize = 0;
  1215. if (ip->i_d.di_version == 3 &&
  1216. (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1217. ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
  1218. mp->m_sb.sb_blocklog;
  1219. else
  1220. ip->i_d.di_cowextsize = 0;
  1221. code = xfs_trans_commit(tp);
  1222. /*
  1223. * Release any dquot(s) the inode had kept before chown.
  1224. */
  1225. xfs_qm_dqrele(olddquot);
  1226. xfs_qm_dqrele(udqp);
  1227. xfs_qm_dqrele(pdqp);
  1228. return code;
  1229. error_trans_cancel:
  1230. xfs_trans_cancel(tp);
  1231. error_free_dquots:
  1232. xfs_qm_dqrele(udqp);
  1233. xfs_qm_dqrele(pdqp);
  1234. return code;
  1235. }
  1236. STATIC int
  1237. xfs_ioc_fssetxattr(
  1238. xfs_inode_t *ip,
  1239. struct file *filp,
  1240. void __user *arg)
  1241. {
  1242. struct fsxattr fa;
  1243. int error;
  1244. if (copy_from_user(&fa, arg, sizeof(fa)))
  1245. return -EFAULT;
  1246. error = mnt_want_write_file(filp);
  1247. if (error)
  1248. return error;
  1249. error = xfs_ioctl_setattr(ip, &fa);
  1250. mnt_drop_write_file(filp);
  1251. return error;
  1252. }
  1253. STATIC int
  1254. xfs_ioc_getxflags(
  1255. xfs_inode_t *ip,
  1256. void __user *arg)
  1257. {
  1258. unsigned int flags;
  1259. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1260. if (copy_to_user(arg, &flags, sizeof(flags)))
  1261. return -EFAULT;
  1262. return 0;
  1263. }
  1264. STATIC int
  1265. xfs_ioc_setxflags(
  1266. struct xfs_inode *ip,
  1267. struct file *filp,
  1268. void __user *arg)
  1269. {
  1270. struct xfs_trans *tp;
  1271. struct fsxattr fa;
  1272. unsigned int flags;
  1273. int join_flags = 0;
  1274. int error;
  1275. if (copy_from_user(&flags, arg, sizeof(flags)))
  1276. return -EFAULT;
  1277. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1278. FS_NOATIME_FL | FS_NODUMP_FL | \
  1279. FS_SYNC_FL))
  1280. return -EOPNOTSUPP;
  1281. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1282. error = mnt_want_write_file(filp);
  1283. if (error)
  1284. return error;
  1285. /*
  1286. * Changing DAX config may require inode locking for mapping
  1287. * invalidation. These need to be held all the way to transaction commit
  1288. * or cancel time, so need to be passed through to
  1289. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1290. * appropriately.
  1291. */
  1292. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1293. if (error)
  1294. goto out_drop_write;
  1295. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1296. if (IS_ERR(tp)) {
  1297. error = PTR_ERR(tp);
  1298. goto out_drop_write;
  1299. }
  1300. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1301. if (error) {
  1302. xfs_trans_cancel(tp);
  1303. goto out_drop_write;
  1304. }
  1305. error = xfs_trans_commit(tp);
  1306. out_drop_write:
  1307. mnt_drop_write_file(filp);
  1308. return error;
  1309. }
  1310. static bool
  1311. xfs_getbmap_format(
  1312. struct kgetbmap *p,
  1313. struct getbmapx __user *u,
  1314. size_t recsize)
  1315. {
  1316. if (put_user(p->bmv_offset, &u->bmv_offset) ||
  1317. put_user(p->bmv_block, &u->bmv_block) ||
  1318. put_user(p->bmv_length, &u->bmv_length) ||
  1319. put_user(0, &u->bmv_count) ||
  1320. put_user(0, &u->bmv_entries))
  1321. return false;
  1322. if (recsize < sizeof(struct getbmapx))
  1323. return true;
  1324. if (put_user(0, &u->bmv_iflags) ||
  1325. put_user(p->bmv_oflags, &u->bmv_oflags) ||
  1326. put_user(0, &u->bmv_unused1) ||
  1327. put_user(0, &u->bmv_unused2))
  1328. return false;
  1329. return true;
  1330. }
  1331. STATIC int
  1332. xfs_ioc_getbmap(
  1333. struct file *file,
  1334. unsigned int cmd,
  1335. void __user *arg)
  1336. {
  1337. struct getbmapx bmx = { 0 };
  1338. struct kgetbmap *buf;
  1339. size_t recsize;
  1340. int error, i;
  1341. switch (cmd) {
  1342. case XFS_IOC_GETBMAPA:
  1343. bmx.bmv_iflags = BMV_IF_ATTRFORK;
  1344. /*FALLTHRU*/
  1345. case XFS_IOC_GETBMAP:
  1346. if (file->f_mode & FMODE_NOCMTIME)
  1347. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1348. /* struct getbmap is a strict subset of struct getbmapx. */
  1349. recsize = sizeof(struct getbmap);
  1350. break;
  1351. case XFS_IOC_GETBMAPX:
  1352. recsize = sizeof(struct getbmapx);
  1353. break;
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. if (copy_from_user(&bmx, arg, recsize))
  1358. return -EFAULT;
  1359. if (bmx.bmv_count < 2)
  1360. return -EINVAL;
  1361. if (bmx.bmv_count > ULONG_MAX / recsize)
  1362. return -ENOMEM;
  1363. buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
  1364. if (!buf)
  1365. return -ENOMEM;
  1366. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
  1367. if (error)
  1368. goto out_free_buf;
  1369. error = -EFAULT;
  1370. if (copy_to_user(arg, &bmx, recsize))
  1371. goto out_free_buf;
  1372. arg += recsize;
  1373. for (i = 0; i < bmx.bmv_entries; i++) {
  1374. if (!xfs_getbmap_format(buf + i, arg, recsize))
  1375. goto out_free_buf;
  1376. arg += recsize;
  1377. }
  1378. error = 0;
  1379. out_free_buf:
  1380. kmem_free(buf);
  1381. return error;
  1382. }
  1383. struct getfsmap_info {
  1384. struct xfs_mount *mp;
  1385. struct fsmap_head __user *data;
  1386. unsigned int idx;
  1387. __u32 last_flags;
  1388. };
  1389. STATIC int
  1390. xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
  1391. {
  1392. struct getfsmap_info *info = priv;
  1393. struct fsmap fm;
  1394. trace_xfs_getfsmap_mapping(info->mp, xfm);
  1395. info->last_flags = xfm->fmr_flags;
  1396. xfs_fsmap_from_internal(&fm, xfm);
  1397. if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
  1398. sizeof(struct fsmap)))
  1399. return -EFAULT;
  1400. return 0;
  1401. }
  1402. STATIC int
  1403. xfs_ioc_getfsmap(
  1404. struct xfs_inode *ip,
  1405. struct fsmap_head __user *arg)
  1406. {
  1407. struct getfsmap_info info = { NULL };
  1408. struct xfs_fsmap_head xhead = {0};
  1409. struct fsmap_head head;
  1410. bool aborted = false;
  1411. int error;
  1412. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  1413. return -EFAULT;
  1414. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  1415. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  1416. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  1417. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  1418. sizeof(head.fmh_keys[1].fmr_reserved)))
  1419. return -EINVAL;
  1420. xhead.fmh_iflags = head.fmh_iflags;
  1421. xhead.fmh_count = head.fmh_count;
  1422. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  1423. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  1424. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1425. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  1426. info.mp = ip->i_mount;
  1427. info.data = arg;
  1428. error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
  1429. if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
  1430. error = 0;
  1431. aborted = true;
  1432. } else if (error)
  1433. return error;
  1434. /* If we didn't abort, set the "last" flag in the last fmx */
  1435. if (!aborted && info.idx) {
  1436. info.last_flags |= FMR_OF_LAST;
  1437. if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
  1438. &info.last_flags, sizeof(info.last_flags)))
  1439. return -EFAULT;
  1440. }
  1441. /* copy back header */
  1442. head.fmh_entries = xhead.fmh_entries;
  1443. head.fmh_oflags = xhead.fmh_oflags;
  1444. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  1445. return -EFAULT;
  1446. return 0;
  1447. }
  1448. STATIC int
  1449. xfs_ioc_scrub_metadata(
  1450. struct xfs_inode *ip,
  1451. void __user *arg)
  1452. {
  1453. struct xfs_scrub_metadata scrub;
  1454. int error;
  1455. if (!capable(CAP_SYS_ADMIN))
  1456. return -EPERM;
  1457. if (copy_from_user(&scrub, arg, sizeof(scrub)))
  1458. return -EFAULT;
  1459. error = xfs_scrub_metadata(ip, &scrub);
  1460. if (error)
  1461. return error;
  1462. if (copy_to_user(arg, &scrub, sizeof(scrub)))
  1463. return -EFAULT;
  1464. return 0;
  1465. }
  1466. int
  1467. xfs_ioc_swapext(
  1468. xfs_swapext_t *sxp)
  1469. {
  1470. xfs_inode_t *ip, *tip;
  1471. struct fd f, tmp;
  1472. int error = 0;
  1473. /* Pull information for the target fd */
  1474. f = fdget((int)sxp->sx_fdtarget);
  1475. if (!f.file) {
  1476. error = -EINVAL;
  1477. goto out;
  1478. }
  1479. if (!(f.file->f_mode & FMODE_WRITE) ||
  1480. !(f.file->f_mode & FMODE_READ) ||
  1481. (f.file->f_flags & O_APPEND)) {
  1482. error = -EBADF;
  1483. goto out_put_file;
  1484. }
  1485. tmp = fdget((int)sxp->sx_fdtmp);
  1486. if (!tmp.file) {
  1487. error = -EINVAL;
  1488. goto out_put_file;
  1489. }
  1490. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1491. !(tmp.file->f_mode & FMODE_READ) ||
  1492. (tmp.file->f_flags & O_APPEND)) {
  1493. error = -EBADF;
  1494. goto out_put_tmp_file;
  1495. }
  1496. if (IS_SWAPFILE(file_inode(f.file)) ||
  1497. IS_SWAPFILE(file_inode(tmp.file))) {
  1498. error = -EINVAL;
  1499. goto out_put_tmp_file;
  1500. }
  1501. /*
  1502. * We need to ensure that the fds passed in point to XFS inodes
  1503. * before we cast and access them as XFS structures as we have no
  1504. * control over what the user passes us here.
  1505. */
  1506. if (f.file->f_op != &xfs_file_operations ||
  1507. tmp.file->f_op != &xfs_file_operations) {
  1508. error = -EINVAL;
  1509. goto out_put_tmp_file;
  1510. }
  1511. ip = XFS_I(file_inode(f.file));
  1512. tip = XFS_I(file_inode(tmp.file));
  1513. if (ip->i_mount != tip->i_mount) {
  1514. error = -EINVAL;
  1515. goto out_put_tmp_file;
  1516. }
  1517. if (ip->i_ino == tip->i_ino) {
  1518. error = -EINVAL;
  1519. goto out_put_tmp_file;
  1520. }
  1521. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1522. error = -EIO;
  1523. goto out_put_tmp_file;
  1524. }
  1525. error = xfs_swap_extents(ip, tip, sxp);
  1526. out_put_tmp_file:
  1527. fdput(tmp);
  1528. out_put_file:
  1529. fdput(f);
  1530. out:
  1531. return error;
  1532. }
  1533. static int
  1534. xfs_ioc_getlabel(
  1535. struct xfs_mount *mp,
  1536. char __user *user_label)
  1537. {
  1538. struct xfs_sb *sbp = &mp->m_sb;
  1539. char label[XFSLABEL_MAX + 1];
  1540. /* Paranoia */
  1541. BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
  1542. /* 1 larger than sb_fname, so this ensures a trailing NUL char */
  1543. memset(label, 0, sizeof(label));
  1544. spin_lock(&mp->m_sb_lock);
  1545. strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
  1546. spin_unlock(&mp->m_sb_lock);
  1547. if (copy_to_user(user_label, label, sizeof(label)))
  1548. return -EFAULT;
  1549. return 0;
  1550. }
  1551. static int
  1552. xfs_ioc_setlabel(
  1553. struct file *filp,
  1554. struct xfs_mount *mp,
  1555. char __user *newlabel)
  1556. {
  1557. struct xfs_sb *sbp = &mp->m_sb;
  1558. char label[XFSLABEL_MAX + 1];
  1559. size_t len;
  1560. int error;
  1561. if (!capable(CAP_SYS_ADMIN))
  1562. return -EPERM;
  1563. /*
  1564. * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
  1565. * smaller, at 12 bytes. We copy one more to be sure we find the
  1566. * (required) NULL character to test the incoming label length.
  1567. * NB: The on disk label doesn't need to be null terminated.
  1568. */
  1569. if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
  1570. return -EFAULT;
  1571. len = strnlen(label, XFSLABEL_MAX + 1);
  1572. if (len > sizeof(sbp->sb_fname))
  1573. return -EINVAL;
  1574. error = mnt_want_write_file(filp);
  1575. if (error)
  1576. return error;
  1577. spin_lock(&mp->m_sb_lock);
  1578. memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
  1579. memcpy(sbp->sb_fname, label, len);
  1580. spin_unlock(&mp->m_sb_lock);
  1581. /*
  1582. * Now we do several things to satisfy userspace.
  1583. * In addition to normal logging of the primary superblock, we also
  1584. * immediately write these changes to sector zero for the primary, then
  1585. * update all backup supers (as xfs_db does for a label change), then
  1586. * invalidate the block device page cache. This is so that any prior
  1587. * buffered reads from userspace (i.e. from blkid) are invalidated,
  1588. * and userspace will see the newly-written label.
  1589. */
  1590. error = xfs_sync_sb_buf(mp);
  1591. if (error)
  1592. goto out;
  1593. /*
  1594. * growfs also updates backup supers so lock against that.
  1595. */
  1596. mutex_lock(&mp->m_growlock);
  1597. error = xfs_update_secondary_sbs(mp);
  1598. mutex_unlock(&mp->m_growlock);
  1599. invalidate_bdev(mp->m_ddev_targp->bt_bdev);
  1600. out:
  1601. mnt_drop_write_file(filp);
  1602. return error;
  1603. }
  1604. /*
  1605. * Note: some of the ioctl's return positive numbers as a
  1606. * byte count indicating success, such as readlink_by_handle.
  1607. * So we don't "sign flip" like most other routines. This means
  1608. * true errors need to be returned as a negative value.
  1609. */
  1610. long
  1611. xfs_file_ioctl(
  1612. struct file *filp,
  1613. unsigned int cmd,
  1614. unsigned long p)
  1615. {
  1616. struct inode *inode = file_inode(filp);
  1617. struct xfs_inode *ip = XFS_I(inode);
  1618. struct xfs_mount *mp = ip->i_mount;
  1619. void __user *arg = (void __user *)p;
  1620. int error;
  1621. trace_xfs_file_ioctl(ip);
  1622. switch (cmd) {
  1623. case FITRIM:
  1624. return xfs_ioc_trim(mp, arg);
  1625. case FS_IOC_GETFSLABEL:
  1626. return xfs_ioc_getlabel(mp, arg);
  1627. case FS_IOC_SETFSLABEL:
  1628. return xfs_ioc_setlabel(filp, mp, arg);
  1629. case XFS_IOC_ALLOCSP:
  1630. case XFS_IOC_FREESP:
  1631. case XFS_IOC_RESVSP:
  1632. case XFS_IOC_UNRESVSP:
  1633. case XFS_IOC_ALLOCSP64:
  1634. case XFS_IOC_FREESP64:
  1635. case XFS_IOC_RESVSP64:
  1636. case XFS_IOC_UNRESVSP64:
  1637. case XFS_IOC_ZERO_RANGE: {
  1638. xfs_flock64_t bf;
  1639. if (copy_from_user(&bf, arg, sizeof(bf)))
  1640. return -EFAULT;
  1641. return xfs_ioc_space(filp, cmd, &bf);
  1642. }
  1643. case XFS_IOC_DIOINFO: {
  1644. struct dioattr da;
  1645. xfs_buftarg_t *target =
  1646. XFS_IS_REALTIME_INODE(ip) ?
  1647. mp->m_rtdev_targp : mp->m_ddev_targp;
  1648. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1649. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1650. if (copy_to_user(arg, &da, sizeof(da)))
  1651. return -EFAULT;
  1652. return 0;
  1653. }
  1654. case XFS_IOC_FSBULKSTAT_SINGLE:
  1655. case XFS_IOC_FSBULKSTAT:
  1656. case XFS_IOC_FSINUMBERS:
  1657. return xfs_ioc_bulkstat(mp, cmd, arg);
  1658. case XFS_IOC_FSGEOMETRY_V1:
  1659. return xfs_ioc_fsgeometry_v1(mp, arg);
  1660. case XFS_IOC_FSGEOMETRY:
  1661. return xfs_ioc_fsgeometry(mp, arg);
  1662. case XFS_IOC_GETVERSION:
  1663. return put_user(inode->i_generation, (int __user *)arg);
  1664. case XFS_IOC_FSGETXATTR:
  1665. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1666. case XFS_IOC_FSGETXATTRA:
  1667. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1668. case XFS_IOC_FSSETXATTR:
  1669. return xfs_ioc_fssetxattr(ip, filp, arg);
  1670. case XFS_IOC_GETXFLAGS:
  1671. return xfs_ioc_getxflags(ip, arg);
  1672. case XFS_IOC_SETXFLAGS:
  1673. return xfs_ioc_setxflags(ip, filp, arg);
  1674. case XFS_IOC_FSSETDM: {
  1675. struct fsdmidata dmi;
  1676. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1677. return -EFAULT;
  1678. error = mnt_want_write_file(filp);
  1679. if (error)
  1680. return error;
  1681. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1682. dmi.fsd_dmstate);
  1683. mnt_drop_write_file(filp);
  1684. return error;
  1685. }
  1686. case XFS_IOC_GETBMAP:
  1687. case XFS_IOC_GETBMAPA:
  1688. case XFS_IOC_GETBMAPX:
  1689. return xfs_ioc_getbmap(filp, cmd, arg);
  1690. case FS_IOC_GETFSMAP:
  1691. return xfs_ioc_getfsmap(ip, arg);
  1692. case XFS_IOC_SCRUB_METADATA:
  1693. return xfs_ioc_scrub_metadata(ip, arg);
  1694. case XFS_IOC_FD_TO_HANDLE:
  1695. case XFS_IOC_PATH_TO_HANDLE:
  1696. case XFS_IOC_PATH_TO_FSHANDLE: {
  1697. xfs_fsop_handlereq_t hreq;
  1698. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1699. return -EFAULT;
  1700. return xfs_find_handle(cmd, &hreq);
  1701. }
  1702. case XFS_IOC_OPEN_BY_HANDLE: {
  1703. xfs_fsop_handlereq_t hreq;
  1704. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1705. return -EFAULT;
  1706. return xfs_open_by_handle(filp, &hreq);
  1707. }
  1708. case XFS_IOC_FSSETDM_BY_HANDLE:
  1709. return xfs_fssetdm_by_handle(filp, arg);
  1710. case XFS_IOC_READLINK_BY_HANDLE: {
  1711. xfs_fsop_handlereq_t hreq;
  1712. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1713. return -EFAULT;
  1714. return xfs_readlink_by_handle(filp, &hreq);
  1715. }
  1716. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1717. return xfs_attrlist_by_handle(filp, arg);
  1718. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1719. return xfs_attrmulti_by_handle(filp, arg);
  1720. case XFS_IOC_SWAPEXT: {
  1721. struct xfs_swapext sxp;
  1722. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1723. return -EFAULT;
  1724. error = mnt_want_write_file(filp);
  1725. if (error)
  1726. return error;
  1727. error = xfs_ioc_swapext(&sxp);
  1728. mnt_drop_write_file(filp);
  1729. return error;
  1730. }
  1731. case XFS_IOC_FSCOUNTS: {
  1732. xfs_fsop_counts_t out;
  1733. error = xfs_fs_counts(mp, &out);
  1734. if (error)
  1735. return error;
  1736. if (copy_to_user(arg, &out, sizeof(out)))
  1737. return -EFAULT;
  1738. return 0;
  1739. }
  1740. case XFS_IOC_SET_RESBLKS: {
  1741. xfs_fsop_resblks_t inout;
  1742. uint64_t in;
  1743. if (!capable(CAP_SYS_ADMIN))
  1744. return -EPERM;
  1745. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1746. return -EROFS;
  1747. if (copy_from_user(&inout, arg, sizeof(inout)))
  1748. return -EFAULT;
  1749. error = mnt_want_write_file(filp);
  1750. if (error)
  1751. return error;
  1752. /* input parameter is passed in resblks field of structure */
  1753. in = inout.resblks;
  1754. error = xfs_reserve_blocks(mp, &in, &inout);
  1755. mnt_drop_write_file(filp);
  1756. if (error)
  1757. return error;
  1758. if (copy_to_user(arg, &inout, sizeof(inout)))
  1759. return -EFAULT;
  1760. return 0;
  1761. }
  1762. case XFS_IOC_GET_RESBLKS: {
  1763. xfs_fsop_resblks_t out;
  1764. if (!capable(CAP_SYS_ADMIN))
  1765. return -EPERM;
  1766. error = xfs_reserve_blocks(mp, NULL, &out);
  1767. if (error)
  1768. return error;
  1769. if (copy_to_user(arg, &out, sizeof(out)))
  1770. return -EFAULT;
  1771. return 0;
  1772. }
  1773. case XFS_IOC_FSGROWFSDATA: {
  1774. xfs_growfs_data_t in;
  1775. if (copy_from_user(&in, arg, sizeof(in)))
  1776. return -EFAULT;
  1777. error = mnt_want_write_file(filp);
  1778. if (error)
  1779. return error;
  1780. error = xfs_growfs_data(mp, &in);
  1781. mnt_drop_write_file(filp);
  1782. return error;
  1783. }
  1784. case XFS_IOC_FSGROWFSLOG: {
  1785. xfs_growfs_log_t in;
  1786. if (copy_from_user(&in, arg, sizeof(in)))
  1787. return -EFAULT;
  1788. error = mnt_want_write_file(filp);
  1789. if (error)
  1790. return error;
  1791. error = xfs_growfs_log(mp, &in);
  1792. mnt_drop_write_file(filp);
  1793. return error;
  1794. }
  1795. case XFS_IOC_FSGROWFSRT: {
  1796. xfs_growfs_rt_t in;
  1797. if (copy_from_user(&in, arg, sizeof(in)))
  1798. return -EFAULT;
  1799. error = mnt_want_write_file(filp);
  1800. if (error)
  1801. return error;
  1802. error = xfs_growfs_rt(mp, &in);
  1803. mnt_drop_write_file(filp);
  1804. return error;
  1805. }
  1806. case XFS_IOC_GOINGDOWN: {
  1807. uint32_t in;
  1808. if (!capable(CAP_SYS_ADMIN))
  1809. return -EPERM;
  1810. if (get_user(in, (uint32_t __user *)arg))
  1811. return -EFAULT;
  1812. return xfs_fs_goingdown(mp, in);
  1813. }
  1814. case XFS_IOC_ERROR_INJECTION: {
  1815. xfs_error_injection_t in;
  1816. if (!capable(CAP_SYS_ADMIN))
  1817. return -EPERM;
  1818. if (copy_from_user(&in, arg, sizeof(in)))
  1819. return -EFAULT;
  1820. return xfs_errortag_add(mp, in.errtag);
  1821. }
  1822. case XFS_IOC_ERROR_CLEARALL:
  1823. if (!capable(CAP_SYS_ADMIN))
  1824. return -EPERM;
  1825. return xfs_errortag_clearall(mp);
  1826. case XFS_IOC_FREE_EOFBLOCKS: {
  1827. struct xfs_fs_eofblocks eofb;
  1828. struct xfs_eofblocks keofb;
  1829. if (!capable(CAP_SYS_ADMIN))
  1830. return -EPERM;
  1831. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1832. return -EROFS;
  1833. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1834. return -EFAULT;
  1835. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1836. if (error)
  1837. return error;
  1838. return xfs_icache_free_eofblocks(mp, &keofb);
  1839. }
  1840. default:
  1841. return -ENOTTY;
  1842. }
  1843. }