xfs_ioctl.c 39 KB

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