xfs_ioctl.c 50 KB

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