xfs_ioctl.c 39 KB

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