xfs_ioctl.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_rtalloc.h"
  29. #include "xfs_itable.h"
  30. #include "xfs_error.h"
  31. #include "xfs_attr.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_fsops.h"
  35. #include "xfs_discard.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_export.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_pnfs.h"
  43. #include <linux/capability.h>
  44. #include <linux/dcache.h>
  45. #include <linux/mount.h>
  46. #include <linux/namei.h>
  47. #include <linux/pagemap.h>
  48. #include <linux/slab.h>
  49. #include <linux/exportfs.h>
  50. /*
  51. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  52. * a file or fs handle.
  53. *
  54. * XFS_IOC_PATH_TO_FSHANDLE
  55. * returns fs handle for a mount point or path within that mount point
  56. * XFS_IOC_FD_TO_HANDLE
  57. * returns full handle for a FD opened in user space
  58. * XFS_IOC_PATH_TO_HANDLE
  59. * returns full handle for a path
  60. */
  61. int
  62. xfs_find_handle(
  63. unsigned int cmd,
  64. xfs_fsop_handlereq_t *hreq)
  65. {
  66. int hsize;
  67. xfs_handle_t handle;
  68. struct inode *inode;
  69. struct fd f = {NULL};
  70. struct path path;
  71. int error;
  72. struct xfs_inode *ip;
  73. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  74. f = fdget(hreq->fd);
  75. if (!f.file)
  76. return -EBADF;
  77. inode = file_inode(f.file);
  78. } else {
  79. error = user_lpath((const char __user *)hreq->path, &path);
  80. if (error)
  81. return error;
  82. inode = d_inode(path.dentry);
  83. }
  84. ip = XFS_I(inode);
  85. /*
  86. * We can only generate handles for inodes residing on a XFS filesystem,
  87. * and only for regular files, directories or symbolic links.
  88. */
  89. error = -EINVAL;
  90. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  91. goto out_put;
  92. error = -EBADF;
  93. if (!S_ISREG(inode->i_mode) &&
  94. !S_ISDIR(inode->i_mode) &&
  95. !S_ISLNK(inode->i_mode))
  96. goto out_put;
  97. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  98. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  99. /*
  100. * This handle only contains an fsid, zero the rest.
  101. */
  102. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  103. hsize = sizeof(xfs_fsid_t);
  104. } else {
  105. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  106. sizeof(handle.ha_fid.fid_len);
  107. handle.ha_fid.fid_pad = 0;
  108. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  109. handle.ha_fid.fid_ino = ip->i_ino;
  110. hsize = XFS_HSIZE(handle);
  111. }
  112. error = -EFAULT;
  113. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  114. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  115. goto out_put;
  116. error = 0;
  117. out_put:
  118. if (cmd == XFS_IOC_FD_TO_HANDLE)
  119. fdput(f);
  120. else
  121. path_put(&path);
  122. return error;
  123. }
  124. /*
  125. * No need to do permission checks on the various pathname components
  126. * as the handle operations are privileged.
  127. */
  128. STATIC int
  129. xfs_handle_acceptable(
  130. void *context,
  131. struct dentry *dentry)
  132. {
  133. return 1;
  134. }
  135. /*
  136. * Convert userspace handle data into a dentry.
  137. */
  138. struct dentry *
  139. xfs_handle_to_dentry(
  140. struct file *parfilp,
  141. void __user *uhandle,
  142. u32 hlen)
  143. {
  144. xfs_handle_t handle;
  145. struct xfs_fid64 fid;
  146. /*
  147. * Only allow handle opens under a directory.
  148. */
  149. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  150. return ERR_PTR(-ENOTDIR);
  151. if (hlen != sizeof(xfs_handle_t))
  152. return ERR_PTR(-EINVAL);
  153. if (copy_from_user(&handle, uhandle, hlen))
  154. return ERR_PTR(-EFAULT);
  155. if (handle.ha_fid.fid_len !=
  156. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  157. return ERR_PTR(-EINVAL);
  158. memset(&fid, 0, sizeof(struct fid));
  159. fid.ino = handle.ha_fid.fid_ino;
  160. fid.gen = handle.ha_fid.fid_gen;
  161. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  162. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  163. xfs_handle_acceptable, NULL);
  164. }
  165. STATIC struct dentry *
  166. xfs_handlereq_to_dentry(
  167. struct file *parfilp,
  168. xfs_fsop_handlereq_t *hreq)
  169. {
  170. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  171. }
  172. int
  173. xfs_open_by_handle(
  174. struct file *parfilp,
  175. xfs_fsop_handlereq_t *hreq)
  176. {
  177. const struct cred *cred = current_cred();
  178. int error;
  179. int fd;
  180. int permflag;
  181. struct file *filp;
  182. struct inode *inode;
  183. struct dentry *dentry;
  184. fmode_t fmode;
  185. struct path path;
  186. if (!capable(CAP_SYS_ADMIN))
  187. return -EPERM;
  188. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  189. if (IS_ERR(dentry))
  190. return PTR_ERR(dentry);
  191. inode = d_inode(dentry);
  192. /* Restrict xfs_open_by_handle to directories & regular files. */
  193. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  194. error = -EPERM;
  195. goto out_dput;
  196. }
  197. #if BITS_PER_LONG != 32
  198. hreq->oflags |= O_LARGEFILE;
  199. #endif
  200. permflag = hreq->oflags;
  201. fmode = OPEN_FMODE(permflag);
  202. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  203. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  204. error = -EPERM;
  205. goto out_dput;
  206. }
  207. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  208. error = -EACCES;
  209. goto out_dput;
  210. }
  211. /* Can't write directories. */
  212. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  213. error = -EISDIR;
  214. goto out_dput;
  215. }
  216. fd = get_unused_fd_flags(0);
  217. if (fd < 0) {
  218. error = fd;
  219. goto out_dput;
  220. }
  221. path.mnt = parfilp->f_path.mnt;
  222. path.dentry = dentry;
  223. filp = dentry_open(&path, hreq->oflags, cred);
  224. dput(dentry);
  225. if (IS_ERR(filp)) {
  226. put_unused_fd(fd);
  227. return PTR_ERR(filp);
  228. }
  229. if (S_ISREG(inode->i_mode)) {
  230. filp->f_flags |= O_NOATIME;
  231. filp->f_mode |= FMODE_NOCMTIME;
  232. }
  233. fd_install(fd, filp);
  234. return fd;
  235. out_dput:
  236. dput(dentry);
  237. return error;
  238. }
  239. int
  240. xfs_readlink_by_handle(
  241. struct file *parfilp,
  242. xfs_fsop_handlereq_t *hreq)
  243. {
  244. struct dentry *dentry;
  245. __u32 olen;
  246. void *link;
  247. int error;
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -EPERM;
  250. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  251. if (IS_ERR(dentry))
  252. return PTR_ERR(dentry);
  253. /* Restrict this handle operation to symlinks only. */
  254. if (!d_is_symlink(dentry)) {
  255. error = -EINVAL;
  256. goto out_dput;
  257. }
  258. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  259. error = -EFAULT;
  260. goto out_dput;
  261. }
  262. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  263. if (!link) {
  264. error = -ENOMEM;
  265. goto out_dput;
  266. }
  267. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  268. if (error)
  269. goto out_kfree;
  270. error = readlink_copy(hreq->ohandle, olen, link);
  271. if (error)
  272. goto out_kfree;
  273. out_kfree:
  274. kfree(link);
  275. out_dput:
  276. dput(dentry);
  277. return error;
  278. }
  279. int
  280. xfs_set_dmattrs(
  281. xfs_inode_t *ip,
  282. u_int evmask,
  283. u_int16_t state)
  284. {
  285. xfs_mount_t *mp = ip->i_mount;
  286. xfs_trans_t *tp;
  287. int error;
  288. if (!capable(CAP_SYS_ADMIN))
  289. return -EPERM;
  290. if (XFS_FORCED_SHUTDOWN(mp))
  291. return -EIO;
  292. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  293. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  294. if (error) {
  295. xfs_trans_cancel(tp);
  296. return error;
  297. }
  298. xfs_ilock(ip, XFS_ILOCK_EXCL);
  299. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  300. ip->i_d.di_dmevmask = evmask;
  301. ip->i_d.di_dmstate = state;
  302. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  303. error = xfs_trans_commit(tp);
  304. return error;
  305. }
  306. STATIC int
  307. xfs_fssetdm_by_handle(
  308. struct file *parfilp,
  309. void __user *arg)
  310. {
  311. int error;
  312. struct fsdmidata fsd;
  313. xfs_fsop_setdm_handlereq_t dmhreq;
  314. struct dentry *dentry;
  315. if (!capable(CAP_MKNOD))
  316. return -EPERM;
  317. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  318. return -EFAULT;
  319. error = mnt_want_write_file(parfilp);
  320. if (error)
  321. return error;
  322. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  323. if (IS_ERR(dentry)) {
  324. mnt_drop_write_file(parfilp);
  325. return PTR_ERR(dentry);
  326. }
  327. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  328. error = -EPERM;
  329. goto out;
  330. }
  331. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  332. error = -EFAULT;
  333. goto out;
  334. }
  335. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  336. fsd.fsd_dmstate);
  337. out:
  338. mnt_drop_write_file(parfilp);
  339. dput(dentry);
  340. return error;
  341. }
  342. STATIC int
  343. xfs_attrlist_by_handle(
  344. struct file *parfilp,
  345. void __user *arg)
  346. {
  347. int error = -ENOMEM;
  348. attrlist_cursor_kern_t *cursor;
  349. xfs_fsop_attrlist_handlereq_t al_hreq;
  350. struct dentry *dentry;
  351. char *kbuf;
  352. if (!capable(CAP_SYS_ADMIN))
  353. return -EPERM;
  354. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  355. return -EFAULT;
  356. if (al_hreq.buflen < sizeof(struct attrlist) ||
  357. al_hreq.buflen > XATTR_LIST_MAX)
  358. return -EINVAL;
  359. /*
  360. * Reject flags, only allow namespaces.
  361. */
  362. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  363. return -EINVAL;
  364. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  365. if (IS_ERR(dentry))
  366. return PTR_ERR(dentry);
  367. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  368. if (!kbuf)
  369. goto out_dput;
  370. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  371. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  372. al_hreq.flags, cursor);
  373. if (error)
  374. goto out_kfree;
  375. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  376. error = -EFAULT;
  377. out_kfree:
  378. kmem_free(kbuf);
  379. out_dput:
  380. dput(dentry);
  381. return error;
  382. }
  383. int
  384. xfs_attrmulti_attr_get(
  385. struct inode *inode,
  386. unsigned char *name,
  387. unsigned char __user *ubuf,
  388. __uint32_t *len,
  389. __uint32_t flags)
  390. {
  391. unsigned char *kbuf;
  392. int error = -EFAULT;
  393. if (*len > XATTR_SIZE_MAX)
  394. return -EINVAL;
  395. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  396. if (!kbuf)
  397. return -ENOMEM;
  398. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  399. if (error)
  400. goto out_kfree;
  401. if (copy_to_user(ubuf, kbuf, *len))
  402. error = -EFAULT;
  403. out_kfree:
  404. kmem_free(kbuf);
  405. return error;
  406. }
  407. int
  408. xfs_attrmulti_attr_set(
  409. struct inode *inode,
  410. unsigned char *name,
  411. const unsigned char __user *ubuf,
  412. __uint32_t len,
  413. __uint32_t flags)
  414. {
  415. unsigned char *kbuf;
  416. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  417. return -EPERM;
  418. if (len > XATTR_SIZE_MAX)
  419. return -EINVAL;
  420. kbuf = memdup_user(ubuf, len);
  421. if (IS_ERR(kbuf))
  422. return PTR_ERR(kbuf);
  423. return xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  424. }
  425. int
  426. xfs_attrmulti_attr_remove(
  427. struct inode *inode,
  428. unsigned char *name,
  429. __uint32_t flags)
  430. {
  431. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  432. return -EPERM;
  433. return xfs_attr_remove(XFS_I(inode), name, flags);
  434. }
  435. STATIC int
  436. xfs_attrmulti_by_handle(
  437. struct file *parfilp,
  438. void __user *arg)
  439. {
  440. int error;
  441. xfs_attr_multiop_t *ops;
  442. xfs_fsop_attrmulti_handlereq_t am_hreq;
  443. struct dentry *dentry;
  444. unsigned int i, size;
  445. unsigned char *attr_name;
  446. if (!capable(CAP_SYS_ADMIN))
  447. return -EPERM;
  448. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  449. return -EFAULT;
  450. /* overflow check */
  451. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  452. return -E2BIG;
  453. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  454. if (IS_ERR(dentry))
  455. return PTR_ERR(dentry);
  456. error = -E2BIG;
  457. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  458. if (!size || size > 16 * PAGE_SIZE)
  459. goto out_dput;
  460. ops = memdup_user(am_hreq.ops, size);
  461. if (IS_ERR(ops)) {
  462. error = PTR_ERR(ops);
  463. goto out_dput;
  464. }
  465. error = -ENOMEM;
  466. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  467. if (!attr_name)
  468. goto out_kfree_ops;
  469. error = 0;
  470. for (i = 0; i < am_hreq.opcount; i++) {
  471. ops[i].am_error = strncpy_from_user((char *)attr_name,
  472. ops[i].am_attrname, MAXNAMELEN);
  473. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  474. error = -ERANGE;
  475. if (ops[i].am_error < 0)
  476. break;
  477. switch (ops[i].am_opcode) {
  478. case ATTR_OP_GET:
  479. ops[i].am_error = xfs_attrmulti_attr_get(
  480. d_inode(dentry), attr_name,
  481. ops[i].am_attrvalue, &ops[i].am_length,
  482. ops[i].am_flags);
  483. break;
  484. case ATTR_OP_SET:
  485. ops[i].am_error = mnt_want_write_file(parfilp);
  486. if (ops[i].am_error)
  487. break;
  488. ops[i].am_error = xfs_attrmulti_attr_set(
  489. d_inode(dentry), attr_name,
  490. ops[i].am_attrvalue, ops[i].am_length,
  491. ops[i].am_flags);
  492. mnt_drop_write_file(parfilp);
  493. break;
  494. case ATTR_OP_REMOVE:
  495. ops[i].am_error = mnt_want_write_file(parfilp);
  496. if (ops[i].am_error)
  497. break;
  498. ops[i].am_error = xfs_attrmulti_attr_remove(
  499. d_inode(dentry), attr_name,
  500. ops[i].am_flags);
  501. mnt_drop_write_file(parfilp);
  502. break;
  503. default:
  504. ops[i].am_error = -EINVAL;
  505. }
  506. }
  507. if (copy_to_user(am_hreq.ops, ops, size))
  508. error = -EFAULT;
  509. kfree(attr_name);
  510. out_kfree_ops:
  511. kfree(ops);
  512. out_dput:
  513. dput(dentry);
  514. return error;
  515. }
  516. int
  517. xfs_ioc_space(
  518. struct xfs_inode *ip,
  519. struct inode *inode,
  520. struct file *filp,
  521. int ioflags,
  522. unsigned int cmd,
  523. xfs_flock64_t *bf)
  524. {
  525. struct iattr iattr;
  526. enum xfs_prealloc_flags flags = 0;
  527. uint iolock = XFS_IOLOCK_EXCL;
  528. int error;
  529. /*
  530. * Only allow the sys admin to reserve space unless
  531. * unwritten extents are enabled.
  532. */
  533. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  534. !capable(CAP_SYS_ADMIN))
  535. return -EPERM;
  536. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  537. return -EPERM;
  538. if (!(filp->f_mode & FMODE_WRITE))
  539. return -EBADF;
  540. if (!S_ISREG(inode->i_mode))
  541. return -EINVAL;
  542. if (filp->f_flags & O_DSYNC)
  543. flags |= XFS_PREALLOC_SYNC;
  544. if (ioflags & XFS_IO_INVIS)
  545. flags |= XFS_PREALLOC_INVISIBLE;
  546. error = mnt_want_write_file(filp);
  547. if (error)
  548. return error;
  549. xfs_ilock(ip, iolock);
  550. error = xfs_break_layouts(inode, &iolock, false);
  551. if (error)
  552. goto out_unlock;
  553. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  554. iolock |= XFS_MMAPLOCK_EXCL;
  555. switch (bf->l_whence) {
  556. case 0: /*SEEK_SET*/
  557. break;
  558. case 1: /*SEEK_CUR*/
  559. bf->l_start += filp->f_pos;
  560. break;
  561. case 2: /*SEEK_END*/
  562. bf->l_start += XFS_ISIZE(ip);
  563. break;
  564. default:
  565. error = -EINVAL;
  566. goto out_unlock;
  567. }
  568. /*
  569. * length of <= 0 for resv/unresv/zero is invalid. length for
  570. * alloc/free is ignored completely and we have no idea what userspace
  571. * might have set it to, so set it to zero to allow range
  572. * checks to pass.
  573. */
  574. switch (cmd) {
  575. case XFS_IOC_ZERO_RANGE:
  576. case XFS_IOC_RESVSP:
  577. case XFS_IOC_RESVSP64:
  578. case XFS_IOC_UNRESVSP:
  579. case XFS_IOC_UNRESVSP64:
  580. if (bf->l_len <= 0) {
  581. error = -EINVAL;
  582. goto out_unlock;
  583. }
  584. break;
  585. default:
  586. bf->l_len = 0;
  587. break;
  588. }
  589. if (bf->l_start < 0 ||
  590. bf->l_start > inode->i_sb->s_maxbytes ||
  591. bf->l_start + bf->l_len < 0 ||
  592. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  593. error = -EINVAL;
  594. goto out_unlock;
  595. }
  596. switch (cmd) {
  597. case XFS_IOC_ZERO_RANGE:
  598. flags |= XFS_PREALLOC_SET;
  599. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  600. break;
  601. case XFS_IOC_RESVSP:
  602. case XFS_IOC_RESVSP64:
  603. flags |= XFS_PREALLOC_SET;
  604. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  605. XFS_BMAPI_PREALLOC);
  606. break;
  607. case XFS_IOC_UNRESVSP:
  608. case XFS_IOC_UNRESVSP64:
  609. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  610. break;
  611. case XFS_IOC_ALLOCSP:
  612. case XFS_IOC_ALLOCSP64:
  613. case XFS_IOC_FREESP:
  614. case XFS_IOC_FREESP64:
  615. flags |= XFS_PREALLOC_CLEAR;
  616. if (bf->l_start > XFS_ISIZE(ip)) {
  617. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  618. bf->l_start - XFS_ISIZE(ip), 0);
  619. if (error)
  620. goto out_unlock;
  621. }
  622. iattr.ia_valid = ATTR_SIZE;
  623. iattr.ia_size = bf->l_start;
  624. error = xfs_setattr_size(ip, &iattr);
  625. break;
  626. default:
  627. ASSERT(0);
  628. error = -EINVAL;
  629. }
  630. if (error)
  631. goto out_unlock;
  632. error = xfs_update_prealloc_flags(ip, flags);
  633. out_unlock:
  634. xfs_iunlock(ip, iolock);
  635. mnt_drop_write_file(filp);
  636. return error;
  637. }
  638. STATIC int
  639. xfs_ioc_bulkstat(
  640. xfs_mount_t *mp,
  641. unsigned int cmd,
  642. void __user *arg)
  643. {
  644. xfs_fsop_bulkreq_t bulkreq;
  645. int count; /* # of records returned */
  646. xfs_ino_t inlast; /* last inode number */
  647. int done;
  648. int error;
  649. /* done = 1 if there are more stats to get and if bulkstat */
  650. /* should be called again (unused here, but used in dmapi) */
  651. if (!capable(CAP_SYS_ADMIN))
  652. return -EPERM;
  653. if (XFS_FORCED_SHUTDOWN(mp))
  654. return -EIO;
  655. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  656. return -EFAULT;
  657. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  658. return -EFAULT;
  659. if ((count = bulkreq.icount) <= 0)
  660. return -EINVAL;
  661. if (bulkreq.ubuffer == NULL)
  662. return -EINVAL;
  663. if (cmd == XFS_IOC_FSINUMBERS)
  664. error = xfs_inumbers(mp, &inlast, &count,
  665. bulkreq.ubuffer, xfs_inumbers_fmt);
  666. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  667. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  668. sizeof(xfs_bstat_t), NULL, &done);
  669. else /* XFS_IOC_FSBULKSTAT */
  670. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  671. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  672. &done);
  673. if (error)
  674. return error;
  675. if (bulkreq.ocount != NULL) {
  676. if (copy_to_user(bulkreq.lastip, &inlast,
  677. sizeof(xfs_ino_t)))
  678. return -EFAULT;
  679. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  680. return -EFAULT;
  681. }
  682. return 0;
  683. }
  684. STATIC int
  685. xfs_ioc_fsgeometry_v1(
  686. xfs_mount_t *mp,
  687. void __user *arg)
  688. {
  689. xfs_fsop_geom_t fsgeo;
  690. int error;
  691. error = xfs_fs_geometry(mp, &fsgeo, 3);
  692. if (error)
  693. return error;
  694. /*
  695. * Caller should have passed an argument of type
  696. * xfs_fsop_geom_v1_t. This is a proper subset of the
  697. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  698. */
  699. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. STATIC int
  704. xfs_ioc_fsgeometry(
  705. xfs_mount_t *mp,
  706. void __user *arg)
  707. {
  708. xfs_fsop_geom_t fsgeo;
  709. int error;
  710. error = xfs_fs_geometry(mp, &fsgeo, 4);
  711. if (error)
  712. return error;
  713. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  714. return -EFAULT;
  715. return 0;
  716. }
  717. /*
  718. * Linux extended inode flags interface.
  719. */
  720. STATIC unsigned int
  721. xfs_merge_ioc_xflags(
  722. unsigned int flags,
  723. unsigned int start)
  724. {
  725. unsigned int xflags = start;
  726. if (flags & FS_IMMUTABLE_FL)
  727. xflags |= XFS_XFLAG_IMMUTABLE;
  728. else
  729. xflags &= ~XFS_XFLAG_IMMUTABLE;
  730. if (flags & FS_APPEND_FL)
  731. xflags |= XFS_XFLAG_APPEND;
  732. else
  733. xflags &= ~XFS_XFLAG_APPEND;
  734. if (flags & FS_SYNC_FL)
  735. xflags |= XFS_XFLAG_SYNC;
  736. else
  737. xflags &= ~XFS_XFLAG_SYNC;
  738. if (flags & FS_NOATIME_FL)
  739. xflags |= XFS_XFLAG_NOATIME;
  740. else
  741. xflags &= ~XFS_XFLAG_NOATIME;
  742. if (flags & FS_NODUMP_FL)
  743. xflags |= XFS_XFLAG_NODUMP;
  744. else
  745. xflags &= ~XFS_XFLAG_NODUMP;
  746. return xflags;
  747. }
  748. STATIC unsigned int
  749. xfs_di2lxflags(
  750. __uint16_t di_flags)
  751. {
  752. unsigned int flags = 0;
  753. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  754. flags |= FS_IMMUTABLE_FL;
  755. if (di_flags & XFS_DIFLAG_APPEND)
  756. flags |= FS_APPEND_FL;
  757. if (di_flags & XFS_DIFLAG_SYNC)
  758. flags |= FS_SYNC_FL;
  759. if (di_flags & XFS_DIFLAG_NOATIME)
  760. flags |= FS_NOATIME_FL;
  761. if (di_flags & XFS_DIFLAG_NODUMP)
  762. flags |= FS_NODUMP_FL;
  763. return flags;
  764. }
  765. STATIC int
  766. xfs_ioc_fsgetxattr(
  767. xfs_inode_t *ip,
  768. int attr,
  769. void __user *arg)
  770. {
  771. struct fsxattr fa;
  772. memset(&fa, 0, sizeof(struct fsxattr));
  773. xfs_ilock(ip, XFS_ILOCK_SHARED);
  774. fa.fsx_xflags = xfs_ip2xflags(ip);
  775. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  776. fa.fsx_projid = xfs_get_projid(ip);
  777. if (attr) {
  778. if (ip->i_afp) {
  779. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  780. fa.fsx_nextents = ip->i_afp->if_bytes /
  781. sizeof(xfs_bmbt_rec_t);
  782. else
  783. fa.fsx_nextents = ip->i_d.di_anextents;
  784. } else
  785. fa.fsx_nextents = 0;
  786. } else {
  787. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  788. fa.fsx_nextents = ip->i_df.if_bytes /
  789. sizeof(xfs_bmbt_rec_t);
  790. else
  791. fa.fsx_nextents = ip->i_d.di_nextents;
  792. }
  793. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  794. if (copy_to_user(arg, &fa, sizeof(fa)))
  795. return -EFAULT;
  796. return 0;
  797. }
  798. STATIC void
  799. xfs_set_diflags(
  800. struct xfs_inode *ip,
  801. unsigned int xflags)
  802. {
  803. unsigned int di_flags;
  804. /* can't set PREALLOC this way, just preserve it */
  805. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  806. if (xflags & XFS_XFLAG_IMMUTABLE)
  807. di_flags |= XFS_DIFLAG_IMMUTABLE;
  808. if (xflags & XFS_XFLAG_APPEND)
  809. di_flags |= XFS_DIFLAG_APPEND;
  810. if (xflags & XFS_XFLAG_SYNC)
  811. di_flags |= XFS_DIFLAG_SYNC;
  812. if (xflags & XFS_XFLAG_NOATIME)
  813. di_flags |= XFS_DIFLAG_NOATIME;
  814. if (xflags & XFS_XFLAG_NODUMP)
  815. di_flags |= XFS_DIFLAG_NODUMP;
  816. if (xflags & XFS_XFLAG_NODEFRAG)
  817. di_flags |= XFS_DIFLAG_NODEFRAG;
  818. if (xflags & XFS_XFLAG_FILESTREAM)
  819. di_flags |= XFS_DIFLAG_FILESTREAM;
  820. if (S_ISDIR(ip->i_d.di_mode)) {
  821. if (xflags & XFS_XFLAG_RTINHERIT)
  822. di_flags |= XFS_DIFLAG_RTINHERIT;
  823. if (xflags & XFS_XFLAG_NOSYMLINKS)
  824. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  825. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  826. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  827. if (xflags & XFS_XFLAG_PROJINHERIT)
  828. di_flags |= XFS_DIFLAG_PROJINHERIT;
  829. } else if (S_ISREG(ip->i_d.di_mode)) {
  830. if (xflags & XFS_XFLAG_REALTIME)
  831. di_flags |= XFS_DIFLAG_REALTIME;
  832. if (xflags & XFS_XFLAG_EXTSIZE)
  833. di_flags |= XFS_DIFLAG_EXTSIZE;
  834. }
  835. ip->i_d.di_flags = di_flags;
  836. }
  837. STATIC void
  838. xfs_diflags_to_linux(
  839. struct xfs_inode *ip)
  840. {
  841. struct inode *inode = VFS_I(ip);
  842. unsigned int xflags = xfs_ip2xflags(ip);
  843. if (xflags & XFS_XFLAG_IMMUTABLE)
  844. inode->i_flags |= S_IMMUTABLE;
  845. else
  846. inode->i_flags &= ~S_IMMUTABLE;
  847. if (xflags & XFS_XFLAG_APPEND)
  848. inode->i_flags |= S_APPEND;
  849. else
  850. inode->i_flags &= ~S_APPEND;
  851. if (xflags & XFS_XFLAG_SYNC)
  852. inode->i_flags |= S_SYNC;
  853. else
  854. inode->i_flags &= ~S_SYNC;
  855. if (xflags & XFS_XFLAG_NOATIME)
  856. inode->i_flags |= S_NOATIME;
  857. else
  858. inode->i_flags &= ~S_NOATIME;
  859. }
  860. static int
  861. xfs_ioctl_setattr_xflags(
  862. struct xfs_trans *tp,
  863. struct xfs_inode *ip,
  864. struct fsxattr *fa)
  865. {
  866. struct xfs_mount *mp = ip->i_mount;
  867. /* Can't change realtime flag if any extents are allocated. */
  868. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  869. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & XFS_XFLAG_REALTIME))
  870. return -EINVAL;
  871. /* If realtime flag is set then must have realtime device */
  872. if (fa->fsx_xflags & XFS_XFLAG_REALTIME) {
  873. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  874. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  875. return -EINVAL;
  876. }
  877. /*
  878. * Can't modify an immutable/append-only file unless
  879. * we have appropriate permission.
  880. */
  881. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  882. (fa->fsx_xflags & (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  883. !capable(CAP_LINUX_IMMUTABLE))
  884. return -EPERM;
  885. xfs_set_diflags(ip, fa->fsx_xflags);
  886. xfs_diflags_to_linux(ip);
  887. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  888. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  889. XFS_STATS_INC(xs_ig_attrchg);
  890. return 0;
  891. }
  892. /*
  893. * Set up the transaction structure for the setattr operation, checking that we
  894. * have permission to do so. On success, return a clean transaction and the
  895. * inode locked exclusively ready for further operation specific checks. On
  896. * failure, return an error without modifying or locking the inode.
  897. */
  898. static struct xfs_trans *
  899. xfs_ioctl_setattr_get_trans(
  900. struct xfs_inode *ip)
  901. {
  902. struct xfs_mount *mp = ip->i_mount;
  903. struct xfs_trans *tp;
  904. int error;
  905. if (mp->m_flags & XFS_MOUNT_RDONLY)
  906. return ERR_PTR(-EROFS);
  907. if (XFS_FORCED_SHUTDOWN(mp))
  908. return ERR_PTR(-EIO);
  909. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  910. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  911. if (error)
  912. goto out_cancel;
  913. xfs_ilock(ip, XFS_ILOCK_EXCL);
  914. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  915. /*
  916. * CAP_FOWNER overrides the following restrictions:
  917. *
  918. * The user ID of the calling process must be equal to the file owner
  919. * ID, except in cases where the CAP_FSETID capability is applicable.
  920. */
  921. if (!inode_owner_or_capable(VFS_I(ip))) {
  922. error = -EPERM;
  923. goto out_cancel;
  924. }
  925. if (mp->m_flags & XFS_MOUNT_WSYNC)
  926. xfs_trans_set_sync(tp);
  927. return tp;
  928. out_cancel:
  929. xfs_trans_cancel(tp);
  930. return ERR_PTR(error);
  931. }
  932. /*
  933. * extent size hint validation is somewhat cumbersome. Rules are:
  934. *
  935. * 1. extent size hint is only valid for directories and regular files
  936. * 2. XFS_XFLAG_EXTSIZE is only valid for regular files
  937. * 3. XFS_XFLAG_EXTSZINHERIT is only valid for directories.
  938. * 4. can only be changed on regular files if no extents are allocated
  939. * 5. can be changed on directories at any time
  940. * 6. extsize hint of 0 turns off hints, clears inode flags.
  941. * 7. Extent size must be a multiple of the appropriate block size.
  942. * 8. for non-realtime files, the extent size hint must be limited
  943. * to half the AG size to avoid alignment extending the extent beyond the
  944. * limits of the AG.
  945. */
  946. static int
  947. xfs_ioctl_setattr_check_extsize(
  948. struct xfs_inode *ip,
  949. struct fsxattr *fa)
  950. {
  951. struct xfs_mount *mp = ip->i_mount;
  952. if ((fa->fsx_xflags & XFS_XFLAG_EXTSIZE) && !S_ISREG(ip->i_d.di_mode))
  953. return -EINVAL;
  954. if ((fa->fsx_xflags & XFS_XFLAG_EXTSZINHERIT) &&
  955. !S_ISDIR(ip->i_d.di_mode))
  956. return -EINVAL;
  957. if (S_ISREG(ip->i_d.di_mode) && ip->i_d.di_nextents &&
  958. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  959. return -EINVAL;
  960. if (fa->fsx_extsize != 0) {
  961. xfs_extlen_t size;
  962. xfs_fsblock_t extsize_fsb;
  963. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  964. if (extsize_fsb > MAXEXTLEN)
  965. return -EINVAL;
  966. if (XFS_IS_REALTIME_INODE(ip) ||
  967. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  968. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  969. } else {
  970. size = mp->m_sb.sb_blocksize;
  971. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  972. return -EINVAL;
  973. }
  974. if (fa->fsx_extsize % size)
  975. return -EINVAL;
  976. } else
  977. fa->fsx_xflags &= ~(XFS_XFLAG_EXTSIZE | XFS_XFLAG_EXTSZINHERIT);
  978. return 0;
  979. }
  980. static int
  981. xfs_ioctl_setattr_check_projid(
  982. struct xfs_inode *ip,
  983. struct fsxattr *fa)
  984. {
  985. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  986. if (fa->fsx_projid > (__uint16_t)-1 &&
  987. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  988. return -EINVAL;
  989. /*
  990. * Project Quota ID state is only allowed to change from within the init
  991. * namespace. Enforce that restriction only if we are trying to change
  992. * the quota ID state. Everything else is allowed in user namespaces.
  993. */
  994. if (current_user_ns() == &init_user_ns)
  995. return 0;
  996. if (xfs_get_projid(ip) != fa->fsx_projid)
  997. return -EINVAL;
  998. if ((fa->fsx_xflags & XFS_XFLAG_PROJINHERIT) !=
  999. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1000. return -EINVAL;
  1001. return 0;
  1002. }
  1003. STATIC int
  1004. xfs_ioctl_setattr(
  1005. xfs_inode_t *ip,
  1006. struct fsxattr *fa)
  1007. {
  1008. struct xfs_mount *mp = ip->i_mount;
  1009. struct xfs_trans *tp;
  1010. struct xfs_dquot *udqp = NULL;
  1011. struct xfs_dquot *pdqp = NULL;
  1012. struct xfs_dquot *olddquot = NULL;
  1013. int code;
  1014. trace_xfs_ioctl_setattr(ip);
  1015. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1016. if (code)
  1017. return code;
  1018. /*
  1019. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1020. * before we start any other transactions. Trying to do this later
  1021. * is messy. We don't care to take a readlock to look at the ids
  1022. * in inode here, because we can't hold it across the trans_reserve.
  1023. * If the IDs do change before we take the ilock, we're covered
  1024. * because the i_*dquot fields will get updated anyway.
  1025. */
  1026. if (XFS_IS_QUOTA_ON(mp)) {
  1027. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1028. ip->i_d.di_gid, fa->fsx_projid,
  1029. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1030. if (code)
  1031. return code;
  1032. }
  1033. tp = xfs_ioctl_setattr_get_trans(ip);
  1034. if (IS_ERR(tp)) {
  1035. code = PTR_ERR(tp);
  1036. goto error_free_dquots;
  1037. }
  1038. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1039. xfs_get_projid(ip) != fa->fsx_projid) {
  1040. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1041. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1042. if (code) /* out of quota */
  1043. goto error_trans_cancel;
  1044. }
  1045. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1046. if (code)
  1047. goto error_trans_cancel;
  1048. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1049. if (code)
  1050. goto error_trans_cancel;
  1051. /*
  1052. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1053. * overrides the following restrictions:
  1054. *
  1055. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1056. * successful return from chown()
  1057. */
  1058. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  1059. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1060. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  1061. /* Change the ownerships and register project quota modifications */
  1062. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1063. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1064. olddquot = xfs_qm_vop_chown(tp, ip,
  1065. &ip->i_pdquot, pdqp);
  1066. }
  1067. ASSERT(ip->i_d.di_version > 1);
  1068. xfs_set_projid(ip, fa->fsx_projid);
  1069. }
  1070. /*
  1071. * Only set the extent size hint if we've already determined that the
  1072. * extent size hint should be set on the inode. If no extent size flags
  1073. * are set on the inode then unconditionally clear the extent size hint.
  1074. */
  1075. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1076. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1077. else
  1078. ip->i_d.di_extsize = 0;
  1079. code = xfs_trans_commit(tp);
  1080. /*
  1081. * Release any dquot(s) the inode had kept before chown.
  1082. */
  1083. xfs_qm_dqrele(olddquot);
  1084. xfs_qm_dqrele(udqp);
  1085. xfs_qm_dqrele(pdqp);
  1086. return code;
  1087. error_trans_cancel:
  1088. xfs_trans_cancel(tp);
  1089. error_free_dquots:
  1090. xfs_qm_dqrele(udqp);
  1091. xfs_qm_dqrele(pdqp);
  1092. return code;
  1093. }
  1094. STATIC int
  1095. xfs_ioc_fssetxattr(
  1096. xfs_inode_t *ip,
  1097. struct file *filp,
  1098. void __user *arg)
  1099. {
  1100. struct fsxattr fa;
  1101. int error;
  1102. if (copy_from_user(&fa, arg, sizeof(fa)))
  1103. return -EFAULT;
  1104. error = mnt_want_write_file(filp);
  1105. if (error)
  1106. return error;
  1107. error = xfs_ioctl_setattr(ip, &fa);
  1108. mnt_drop_write_file(filp);
  1109. return error;
  1110. }
  1111. STATIC int
  1112. xfs_ioc_getxflags(
  1113. xfs_inode_t *ip,
  1114. void __user *arg)
  1115. {
  1116. unsigned int flags;
  1117. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1118. if (copy_to_user(arg, &flags, sizeof(flags)))
  1119. return -EFAULT;
  1120. return 0;
  1121. }
  1122. STATIC int
  1123. xfs_ioc_setxflags(
  1124. struct xfs_inode *ip,
  1125. struct file *filp,
  1126. void __user *arg)
  1127. {
  1128. struct xfs_trans *tp;
  1129. struct fsxattr fa;
  1130. unsigned int flags;
  1131. int error;
  1132. if (copy_from_user(&flags, arg, sizeof(flags)))
  1133. return -EFAULT;
  1134. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1135. FS_NOATIME_FL | FS_NODUMP_FL | \
  1136. FS_SYNC_FL))
  1137. return -EOPNOTSUPP;
  1138. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1139. error = mnt_want_write_file(filp);
  1140. if (error)
  1141. return error;
  1142. tp = xfs_ioctl_setattr_get_trans(ip);
  1143. if (IS_ERR(tp)) {
  1144. error = PTR_ERR(tp);
  1145. goto out_drop_write;
  1146. }
  1147. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1148. if (error) {
  1149. xfs_trans_cancel(tp);
  1150. goto out_drop_write;
  1151. }
  1152. error = xfs_trans_commit(tp);
  1153. out_drop_write:
  1154. mnt_drop_write_file(filp);
  1155. return error;
  1156. }
  1157. STATIC int
  1158. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1159. {
  1160. struct getbmap __user *base = (struct getbmap __user *)*ap;
  1161. /* copy only getbmap portion (not getbmapx) */
  1162. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1163. return -EFAULT;
  1164. *ap += sizeof(struct getbmap);
  1165. return 0;
  1166. }
  1167. STATIC int
  1168. xfs_ioc_getbmap(
  1169. struct xfs_inode *ip,
  1170. int ioflags,
  1171. unsigned int cmd,
  1172. void __user *arg)
  1173. {
  1174. struct getbmapx bmx;
  1175. int error;
  1176. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1177. return -EFAULT;
  1178. if (bmx.bmv_count < 2)
  1179. return -EINVAL;
  1180. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1181. if (ioflags & XFS_IO_INVIS)
  1182. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1183. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1184. (__force struct getbmap *)arg+1);
  1185. if (error)
  1186. return error;
  1187. /* copy back header - only size of getbmap */
  1188. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1189. return -EFAULT;
  1190. return 0;
  1191. }
  1192. STATIC int
  1193. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1194. {
  1195. struct getbmapx __user *base = (struct getbmapx __user *)*ap;
  1196. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1197. return -EFAULT;
  1198. *ap += sizeof(struct getbmapx);
  1199. return 0;
  1200. }
  1201. STATIC int
  1202. xfs_ioc_getbmapx(
  1203. struct xfs_inode *ip,
  1204. void __user *arg)
  1205. {
  1206. struct getbmapx bmx;
  1207. int error;
  1208. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1209. return -EFAULT;
  1210. if (bmx.bmv_count < 2)
  1211. return -EINVAL;
  1212. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1213. return -EINVAL;
  1214. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1215. (__force struct getbmapx *)arg+1);
  1216. if (error)
  1217. return error;
  1218. /* copy back header */
  1219. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1220. return -EFAULT;
  1221. return 0;
  1222. }
  1223. int
  1224. xfs_ioc_swapext(
  1225. xfs_swapext_t *sxp)
  1226. {
  1227. xfs_inode_t *ip, *tip;
  1228. struct fd f, tmp;
  1229. int error = 0;
  1230. /* Pull information for the target fd */
  1231. f = fdget((int)sxp->sx_fdtarget);
  1232. if (!f.file) {
  1233. error = -EINVAL;
  1234. goto out;
  1235. }
  1236. if (!(f.file->f_mode & FMODE_WRITE) ||
  1237. !(f.file->f_mode & FMODE_READ) ||
  1238. (f.file->f_flags & O_APPEND)) {
  1239. error = -EBADF;
  1240. goto out_put_file;
  1241. }
  1242. tmp = fdget((int)sxp->sx_fdtmp);
  1243. if (!tmp.file) {
  1244. error = -EINVAL;
  1245. goto out_put_file;
  1246. }
  1247. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1248. !(tmp.file->f_mode & FMODE_READ) ||
  1249. (tmp.file->f_flags & O_APPEND)) {
  1250. error = -EBADF;
  1251. goto out_put_tmp_file;
  1252. }
  1253. if (IS_SWAPFILE(file_inode(f.file)) ||
  1254. IS_SWAPFILE(file_inode(tmp.file))) {
  1255. error = -EINVAL;
  1256. goto out_put_tmp_file;
  1257. }
  1258. ip = XFS_I(file_inode(f.file));
  1259. tip = XFS_I(file_inode(tmp.file));
  1260. if (ip->i_mount != tip->i_mount) {
  1261. error = -EINVAL;
  1262. goto out_put_tmp_file;
  1263. }
  1264. if (ip->i_ino == tip->i_ino) {
  1265. error = -EINVAL;
  1266. goto out_put_tmp_file;
  1267. }
  1268. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1269. error = -EIO;
  1270. goto out_put_tmp_file;
  1271. }
  1272. error = xfs_swap_extents(ip, tip, sxp);
  1273. out_put_tmp_file:
  1274. fdput(tmp);
  1275. out_put_file:
  1276. fdput(f);
  1277. out:
  1278. return error;
  1279. }
  1280. /*
  1281. * Note: some of the ioctl's return positive numbers as a
  1282. * byte count indicating success, such as readlink_by_handle.
  1283. * So we don't "sign flip" like most other routines. This means
  1284. * true errors need to be returned as a negative value.
  1285. */
  1286. long
  1287. xfs_file_ioctl(
  1288. struct file *filp,
  1289. unsigned int cmd,
  1290. unsigned long p)
  1291. {
  1292. struct inode *inode = file_inode(filp);
  1293. struct xfs_inode *ip = XFS_I(inode);
  1294. struct xfs_mount *mp = ip->i_mount;
  1295. void __user *arg = (void __user *)p;
  1296. int ioflags = 0;
  1297. int error;
  1298. if (filp->f_mode & FMODE_NOCMTIME)
  1299. ioflags |= XFS_IO_INVIS;
  1300. trace_xfs_file_ioctl(ip);
  1301. switch (cmd) {
  1302. case FITRIM:
  1303. return xfs_ioc_trim(mp, arg);
  1304. case XFS_IOC_ALLOCSP:
  1305. case XFS_IOC_FREESP:
  1306. case XFS_IOC_RESVSP:
  1307. case XFS_IOC_UNRESVSP:
  1308. case XFS_IOC_ALLOCSP64:
  1309. case XFS_IOC_FREESP64:
  1310. case XFS_IOC_RESVSP64:
  1311. case XFS_IOC_UNRESVSP64:
  1312. case XFS_IOC_ZERO_RANGE: {
  1313. xfs_flock64_t bf;
  1314. if (copy_from_user(&bf, arg, sizeof(bf)))
  1315. return -EFAULT;
  1316. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1317. }
  1318. case XFS_IOC_DIOINFO: {
  1319. struct dioattr da;
  1320. xfs_buftarg_t *target =
  1321. XFS_IS_REALTIME_INODE(ip) ?
  1322. mp->m_rtdev_targp : mp->m_ddev_targp;
  1323. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1324. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1325. if (copy_to_user(arg, &da, sizeof(da)))
  1326. return -EFAULT;
  1327. return 0;
  1328. }
  1329. case XFS_IOC_FSBULKSTAT_SINGLE:
  1330. case XFS_IOC_FSBULKSTAT:
  1331. case XFS_IOC_FSINUMBERS:
  1332. return xfs_ioc_bulkstat(mp, cmd, arg);
  1333. case XFS_IOC_FSGEOMETRY_V1:
  1334. return xfs_ioc_fsgeometry_v1(mp, arg);
  1335. case XFS_IOC_FSGEOMETRY:
  1336. return xfs_ioc_fsgeometry(mp, arg);
  1337. case XFS_IOC_GETVERSION:
  1338. return put_user(inode->i_generation, (int __user *)arg);
  1339. case XFS_IOC_FSGETXATTR:
  1340. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1341. case XFS_IOC_FSGETXATTRA:
  1342. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1343. case XFS_IOC_FSSETXATTR:
  1344. return xfs_ioc_fssetxattr(ip, filp, arg);
  1345. case XFS_IOC_GETXFLAGS:
  1346. return xfs_ioc_getxflags(ip, arg);
  1347. case XFS_IOC_SETXFLAGS:
  1348. return xfs_ioc_setxflags(ip, filp, arg);
  1349. case XFS_IOC_FSSETDM: {
  1350. struct fsdmidata dmi;
  1351. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1352. return -EFAULT;
  1353. error = mnt_want_write_file(filp);
  1354. if (error)
  1355. return error;
  1356. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1357. dmi.fsd_dmstate);
  1358. mnt_drop_write_file(filp);
  1359. return error;
  1360. }
  1361. case XFS_IOC_GETBMAP:
  1362. case XFS_IOC_GETBMAPA:
  1363. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1364. case XFS_IOC_GETBMAPX:
  1365. return xfs_ioc_getbmapx(ip, arg);
  1366. case XFS_IOC_FD_TO_HANDLE:
  1367. case XFS_IOC_PATH_TO_HANDLE:
  1368. case XFS_IOC_PATH_TO_FSHANDLE: {
  1369. xfs_fsop_handlereq_t hreq;
  1370. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1371. return -EFAULT;
  1372. return xfs_find_handle(cmd, &hreq);
  1373. }
  1374. case XFS_IOC_OPEN_BY_HANDLE: {
  1375. xfs_fsop_handlereq_t hreq;
  1376. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1377. return -EFAULT;
  1378. return xfs_open_by_handle(filp, &hreq);
  1379. }
  1380. case XFS_IOC_FSSETDM_BY_HANDLE:
  1381. return xfs_fssetdm_by_handle(filp, arg);
  1382. case XFS_IOC_READLINK_BY_HANDLE: {
  1383. xfs_fsop_handlereq_t hreq;
  1384. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1385. return -EFAULT;
  1386. return xfs_readlink_by_handle(filp, &hreq);
  1387. }
  1388. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1389. return xfs_attrlist_by_handle(filp, arg);
  1390. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1391. return xfs_attrmulti_by_handle(filp, arg);
  1392. case XFS_IOC_SWAPEXT: {
  1393. struct xfs_swapext sxp;
  1394. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1395. return -EFAULT;
  1396. error = mnt_want_write_file(filp);
  1397. if (error)
  1398. return error;
  1399. error = xfs_ioc_swapext(&sxp);
  1400. mnt_drop_write_file(filp);
  1401. return error;
  1402. }
  1403. case XFS_IOC_FSCOUNTS: {
  1404. xfs_fsop_counts_t out;
  1405. error = xfs_fs_counts(mp, &out);
  1406. if (error)
  1407. return error;
  1408. if (copy_to_user(arg, &out, sizeof(out)))
  1409. return -EFAULT;
  1410. return 0;
  1411. }
  1412. case XFS_IOC_SET_RESBLKS: {
  1413. xfs_fsop_resblks_t inout;
  1414. __uint64_t in;
  1415. if (!capable(CAP_SYS_ADMIN))
  1416. return -EPERM;
  1417. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1418. return -EROFS;
  1419. if (copy_from_user(&inout, arg, sizeof(inout)))
  1420. return -EFAULT;
  1421. error = mnt_want_write_file(filp);
  1422. if (error)
  1423. return error;
  1424. /* input parameter is passed in resblks field of structure */
  1425. in = inout.resblks;
  1426. error = xfs_reserve_blocks(mp, &in, &inout);
  1427. mnt_drop_write_file(filp);
  1428. if (error)
  1429. return error;
  1430. if (copy_to_user(arg, &inout, sizeof(inout)))
  1431. return -EFAULT;
  1432. return 0;
  1433. }
  1434. case XFS_IOC_GET_RESBLKS: {
  1435. xfs_fsop_resblks_t out;
  1436. if (!capable(CAP_SYS_ADMIN))
  1437. return -EPERM;
  1438. error = xfs_reserve_blocks(mp, NULL, &out);
  1439. if (error)
  1440. return error;
  1441. if (copy_to_user(arg, &out, sizeof(out)))
  1442. return -EFAULT;
  1443. return 0;
  1444. }
  1445. case XFS_IOC_FSGROWFSDATA: {
  1446. xfs_growfs_data_t in;
  1447. if (copy_from_user(&in, arg, sizeof(in)))
  1448. return -EFAULT;
  1449. error = mnt_want_write_file(filp);
  1450. if (error)
  1451. return error;
  1452. error = xfs_growfs_data(mp, &in);
  1453. mnt_drop_write_file(filp);
  1454. return error;
  1455. }
  1456. case XFS_IOC_FSGROWFSLOG: {
  1457. xfs_growfs_log_t in;
  1458. if (copy_from_user(&in, arg, sizeof(in)))
  1459. return -EFAULT;
  1460. error = mnt_want_write_file(filp);
  1461. if (error)
  1462. return error;
  1463. error = xfs_growfs_log(mp, &in);
  1464. mnt_drop_write_file(filp);
  1465. return error;
  1466. }
  1467. case XFS_IOC_FSGROWFSRT: {
  1468. xfs_growfs_rt_t in;
  1469. if (copy_from_user(&in, arg, sizeof(in)))
  1470. return -EFAULT;
  1471. error = mnt_want_write_file(filp);
  1472. if (error)
  1473. return error;
  1474. error = xfs_growfs_rt(mp, &in);
  1475. mnt_drop_write_file(filp);
  1476. return error;
  1477. }
  1478. case XFS_IOC_GOINGDOWN: {
  1479. __uint32_t in;
  1480. if (!capable(CAP_SYS_ADMIN))
  1481. return -EPERM;
  1482. if (get_user(in, (__uint32_t __user *)arg))
  1483. return -EFAULT;
  1484. return xfs_fs_goingdown(mp, in);
  1485. }
  1486. case XFS_IOC_ERROR_INJECTION: {
  1487. xfs_error_injection_t in;
  1488. if (!capable(CAP_SYS_ADMIN))
  1489. return -EPERM;
  1490. if (copy_from_user(&in, arg, sizeof(in)))
  1491. return -EFAULT;
  1492. return xfs_errortag_add(in.errtag, mp);
  1493. }
  1494. case XFS_IOC_ERROR_CLEARALL:
  1495. if (!capable(CAP_SYS_ADMIN))
  1496. return -EPERM;
  1497. return xfs_errortag_clearall(mp, 1);
  1498. case XFS_IOC_FREE_EOFBLOCKS: {
  1499. struct xfs_fs_eofblocks eofb;
  1500. struct xfs_eofblocks keofb;
  1501. if (!capable(CAP_SYS_ADMIN))
  1502. return -EPERM;
  1503. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1504. return -EROFS;
  1505. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1506. return -EFAULT;
  1507. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1508. if (error)
  1509. return error;
  1510. return xfs_icache_free_eofblocks(mp, &keofb);
  1511. }
  1512. default:
  1513. return -ENOTTY;
  1514. }
  1515. }