xfs_ioctl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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 -XFS_ERROR(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 = -XFS_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 = -XFS_ERROR(EPERM);
  207. goto out_dput;
  208. }
  209. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  210. error = -XFS_ERROR(EACCES);
  211. goto out_dput;
  212. }
  213. /* Can't write directories. */
  214. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  215. error = -XFS_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 -XFS_ERROR(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 = -XFS_ERROR(EINVAL);
  258. goto out_dput;
  259. }
  260. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  261. error = -XFS_ERROR(EFAULT);
  262. goto out_dput;
  263. }
  264. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  265. if (!link) {
  266. error = -XFS_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 XFS_ERROR(EPERM);
  292. if (XFS_FORCED_SHUTDOWN(mp))
  293. return XFS_ERROR(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 -XFS_ERROR(EPERM);
  319. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  320. return -XFS_ERROR(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 = -XFS_ERROR(EPERM);
  331. goto out;
  332. }
  333. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  334. error = -XFS_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 -XFS_ERROR(EPERM);
  356. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  357. return -XFS_ERROR(EFAULT);
  358. if (al_hreq.buflen < sizeof(struct attrlist) ||
  359. al_hreq.buflen > XATTR_LIST_MAX)
  360. return -XFS_ERROR(EINVAL);
  361. /*
  362. * Reject flags, only allow namespaces.
  363. */
  364. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  365. return -XFS_ERROR(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. int error = EFAULT;
  419. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  420. return EPERM;
  421. if (len > XATTR_SIZE_MAX)
  422. return EINVAL;
  423. kbuf = memdup_user(ubuf, len);
  424. if (IS_ERR(kbuf))
  425. return PTR_ERR(kbuf);
  426. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  427. return error;
  428. }
  429. int
  430. xfs_attrmulti_attr_remove(
  431. struct inode *inode,
  432. unsigned char *name,
  433. __uint32_t flags)
  434. {
  435. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  436. return EPERM;
  437. return xfs_attr_remove(XFS_I(inode), name, flags);
  438. }
  439. STATIC int
  440. xfs_attrmulti_by_handle(
  441. struct file *parfilp,
  442. void __user *arg)
  443. {
  444. int error;
  445. xfs_attr_multiop_t *ops;
  446. xfs_fsop_attrmulti_handlereq_t am_hreq;
  447. struct dentry *dentry;
  448. unsigned int i, size;
  449. unsigned char *attr_name;
  450. if (!capable(CAP_SYS_ADMIN))
  451. return -XFS_ERROR(EPERM);
  452. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  453. return -XFS_ERROR(EFAULT);
  454. /* overflow check */
  455. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  456. return -E2BIG;
  457. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  458. if (IS_ERR(dentry))
  459. return PTR_ERR(dentry);
  460. error = E2BIG;
  461. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  462. if (!size || size > 16 * PAGE_SIZE)
  463. goto out_dput;
  464. ops = memdup_user(am_hreq.ops, size);
  465. if (IS_ERR(ops)) {
  466. error = -PTR_ERR(ops);
  467. goto out_dput;
  468. }
  469. error = ENOMEM;
  470. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  471. if (!attr_name)
  472. goto out_kfree_ops;
  473. error = 0;
  474. for (i = 0; i < am_hreq.opcount; i++) {
  475. ops[i].am_error = strncpy_from_user((char *)attr_name,
  476. ops[i].am_attrname, MAXNAMELEN);
  477. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  478. error = ERANGE;
  479. if (ops[i].am_error < 0)
  480. break;
  481. switch (ops[i].am_opcode) {
  482. case ATTR_OP_GET:
  483. ops[i].am_error = xfs_attrmulti_attr_get(
  484. dentry->d_inode, attr_name,
  485. ops[i].am_attrvalue, &ops[i].am_length,
  486. ops[i].am_flags);
  487. break;
  488. case ATTR_OP_SET:
  489. ops[i].am_error = mnt_want_write_file(parfilp);
  490. if (ops[i].am_error)
  491. break;
  492. ops[i].am_error = xfs_attrmulti_attr_set(
  493. dentry->d_inode, attr_name,
  494. ops[i].am_attrvalue, ops[i].am_length,
  495. ops[i].am_flags);
  496. mnt_drop_write_file(parfilp);
  497. break;
  498. case ATTR_OP_REMOVE:
  499. ops[i].am_error = mnt_want_write_file(parfilp);
  500. if (ops[i].am_error)
  501. break;
  502. ops[i].am_error = xfs_attrmulti_attr_remove(
  503. dentry->d_inode, attr_name,
  504. ops[i].am_flags);
  505. mnt_drop_write_file(parfilp);
  506. break;
  507. default:
  508. ops[i].am_error = EINVAL;
  509. }
  510. }
  511. if (copy_to_user(am_hreq.ops, ops, size))
  512. error = XFS_ERROR(EFAULT);
  513. kfree(attr_name);
  514. out_kfree_ops:
  515. kfree(ops);
  516. out_dput:
  517. dput(dentry);
  518. return -error;
  519. }
  520. int
  521. xfs_ioc_space(
  522. struct xfs_inode *ip,
  523. struct inode *inode,
  524. struct file *filp,
  525. int ioflags,
  526. unsigned int cmd,
  527. xfs_flock64_t *bf)
  528. {
  529. struct xfs_mount *mp = ip->i_mount;
  530. struct xfs_trans *tp;
  531. struct iattr iattr;
  532. bool setprealloc = false;
  533. bool clrprealloc = false;
  534. int error;
  535. /*
  536. * Only allow the sys admin to reserve space unless
  537. * unwritten extents are enabled.
  538. */
  539. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  540. !capable(CAP_SYS_ADMIN))
  541. return -XFS_ERROR(EPERM);
  542. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  543. return -XFS_ERROR(EPERM);
  544. if (!(filp->f_mode & FMODE_WRITE))
  545. return -XFS_ERROR(EBADF);
  546. if (!S_ISREG(inode->i_mode))
  547. return -XFS_ERROR(EINVAL);
  548. error = mnt_want_write_file(filp);
  549. if (error)
  550. return error;
  551. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  552. switch (bf->l_whence) {
  553. case 0: /*SEEK_SET*/
  554. break;
  555. case 1: /*SEEK_CUR*/
  556. bf->l_start += filp->f_pos;
  557. break;
  558. case 2: /*SEEK_END*/
  559. bf->l_start += XFS_ISIZE(ip);
  560. break;
  561. default:
  562. error = XFS_ERROR(EINVAL);
  563. goto out_unlock;
  564. }
  565. /*
  566. * length of <= 0 for resv/unresv/zero is invalid. length for
  567. * alloc/free is ignored completely and we have no idea what userspace
  568. * might have set it to, so set it to zero to allow range
  569. * checks to pass.
  570. */
  571. switch (cmd) {
  572. case XFS_IOC_ZERO_RANGE:
  573. case XFS_IOC_RESVSP:
  574. case XFS_IOC_RESVSP64:
  575. case XFS_IOC_UNRESVSP:
  576. case XFS_IOC_UNRESVSP64:
  577. if (bf->l_len <= 0) {
  578. error = XFS_ERROR(EINVAL);
  579. goto out_unlock;
  580. }
  581. break;
  582. default:
  583. bf->l_len = 0;
  584. break;
  585. }
  586. if (bf->l_start < 0 ||
  587. bf->l_start > mp->m_super->s_maxbytes ||
  588. bf->l_start + bf->l_len < 0 ||
  589. bf->l_start + bf->l_len >= mp->m_super->s_maxbytes) {
  590. error = XFS_ERROR(EINVAL);
  591. goto out_unlock;
  592. }
  593. switch (cmd) {
  594. case XFS_IOC_ZERO_RANGE:
  595. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  596. if (!error)
  597. setprealloc = true;
  598. break;
  599. case XFS_IOC_RESVSP:
  600. case XFS_IOC_RESVSP64:
  601. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  602. XFS_BMAPI_PREALLOC);
  603. if (!error)
  604. setprealloc = true;
  605. break;
  606. case XFS_IOC_UNRESVSP:
  607. case XFS_IOC_UNRESVSP64:
  608. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  609. break;
  610. case XFS_IOC_ALLOCSP:
  611. case XFS_IOC_ALLOCSP64:
  612. case XFS_IOC_FREESP:
  613. case XFS_IOC_FREESP64:
  614. if (bf->l_start > XFS_ISIZE(ip)) {
  615. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  616. bf->l_start - XFS_ISIZE(ip), 0);
  617. if (error)
  618. goto out_unlock;
  619. }
  620. iattr.ia_valid = ATTR_SIZE;
  621. iattr.ia_size = bf->l_start;
  622. error = xfs_setattr_size(ip, &iattr);
  623. if (!error)
  624. clrprealloc = true;
  625. break;
  626. default:
  627. ASSERT(0);
  628. error = XFS_ERROR(EINVAL);
  629. }
  630. if (error)
  631. goto out_unlock;
  632. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  633. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_writeid, 0, 0);
  634. if (error) {
  635. xfs_trans_cancel(tp, 0);
  636. goto out_unlock;
  637. }
  638. xfs_ilock(ip, XFS_ILOCK_EXCL);
  639. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  640. if (!(ioflags & IO_INVIS)) {
  641. ip->i_d.di_mode &= ~S_ISUID;
  642. if (ip->i_d.di_mode & S_IXGRP)
  643. ip->i_d.di_mode &= ~S_ISGID;
  644. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  645. }
  646. if (setprealloc)
  647. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  648. else if (clrprealloc)
  649. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  650. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  651. if (filp->f_flags & O_DSYNC)
  652. xfs_trans_set_sync(tp);
  653. error = xfs_trans_commit(tp, 0);
  654. out_unlock:
  655. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  656. mnt_drop_write_file(filp);
  657. return -error;
  658. }
  659. STATIC int
  660. xfs_ioc_bulkstat(
  661. xfs_mount_t *mp,
  662. unsigned int cmd,
  663. void __user *arg)
  664. {
  665. xfs_fsop_bulkreq_t bulkreq;
  666. int count; /* # of records returned */
  667. xfs_ino_t inlast; /* last inode number */
  668. int done;
  669. int error;
  670. /* done = 1 if there are more stats to get and if bulkstat */
  671. /* should be called again (unused here, but used in dmapi) */
  672. if (!capable(CAP_SYS_ADMIN))
  673. return -EPERM;
  674. if (XFS_FORCED_SHUTDOWN(mp))
  675. return -XFS_ERROR(EIO);
  676. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  677. return -XFS_ERROR(EFAULT);
  678. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  679. return -XFS_ERROR(EFAULT);
  680. if ((count = bulkreq.icount) <= 0)
  681. return -XFS_ERROR(EINVAL);
  682. if (bulkreq.ubuffer == NULL)
  683. return -XFS_ERROR(EINVAL);
  684. if (cmd == XFS_IOC_FSINUMBERS)
  685. error = xfs_inumbers(mp, &inlast, &count,
  686. bulkreq.ubuffer, xfs_inumbers_fmt);
  687. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  688. error = xfs_bulkstat_single(mp, &inlast,
  689. bulkreq.ubuffer, &done);
  690. else /* XFS_IOC_FSBULKSTAT */
  691. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  692. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  693. &done);
  694. if (error)
  695. return -error;
  696. if (bulkreq.ocount != NULL) {
  697. if (copy_to_user(bulkreq.lastip, &inlast,
  698. sizeof(xfs_ino_t)))
  699. return -XFS_ERROR(EFAULT);
  700. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  701. return -XFS_ERROR(EFAULT);
  702. }
  703. return 0;
  704. }
  705. STATIC int
  706. xfs_ioc_fsgeometry_v1(
  707. xfs_mount_t *mp,
  708. void __user *arg)
  709. {
  710. xfs_fsop_geom_t fsgeo;
  711. int error;
  712. error = xfs_fs_geometry(mp, &fsgeo, 3);
  713. if (error)
  714. return -error;
  715. /*
  716. * Caller should have passed an argument of type
  717. * xfs_fsop_geom_v1_t. This is a proper subset of the
  718. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  719. */
  720. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  721. return -XFS_ERROR(EFAULT);
  722. return 0;
  723. }
  724. STATIC int
  725. xfs_ioc_fsgeometry(
  726. xfs_mount_t *mp,
  727. void __user *arg)
  728. {
  729. xfs_fsop_geom_t fsgeo;
  730. int error;
  731. error = xfs_fs_geometry(mp, &fsgeo, 4);
  732. if (error)
  733. return -error;
  734. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  735. return -XFS_ERROR(EFAULT);
  736. return 0;
  737. }
  738. /*
  739. * Linux extended inode flags interface.
  740. */
  741. STATIC unsigned int
  742. xfs_merge_ioc_xflags(
  743. unsigned int flags,
  744. unsigned int start)
  745. {
  746. unsigned int xflags = start;
  747. if (flags & FS_IMMUTABLE_FL)
  748. xflags |= XFS_XFLAG_IMMUTABLE;
  749. else
  750. xflags &= ~XFS_XFLAG_IMMUTABLE;
  751. if (flags & FS_APPEND_FL)
  752. xflags |= XFS_XFLAG_APPEND;
  753. else
  754. xflags &= ~XFS_XFLAG_APPEND;
  755. if (flags & FS_SYNC_FL)
  756. xflags |= XFS_XFLAG_SYNC;
  757. else
  758. xflags &= ~XFS_XFLAG_SYNC;
  759. if (flags & FS_NOATIME_FL)
  760. xflags |= XFS_XFLAG_NOATIME;
  761. else
  762. xflags &= ~XFS_XFLAG_NOATIME;
  763. if (flags & FS_NODUMP_FL)
  764. xflags |= XFS_XFLAG_NODUMP;
  765. else
  766. xflags &= ~XFS_XFLAG_NODUMP;
  767. return xflags;
  768. }
  769. STATIC unsigned int
  770. xfs_di2lxflags(
  771. __uint16_t di_flags)
  772. {
  773. unsigned int flags = 0;
  774. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  775. flags |= FS_IMMUTABLE_FL;
  776. if (di_flags & XFS_DIFLAG_APPEND)
  777. flags |= FS_APPEND_FL;
  778. if (di_flags & XFS_DIFLAG_SYNC)
  779. flags |= FS_SYNC_FL;
  780. if (di_flags & XFS_DIFLAG_NOATIME)
  781. flags |= FS_NOATIME_FL;
  782. if (di_flags & XFS_DIFLAG_NODUMP)
  783. flags |= FS_NODUMP_FL;
  784. return flags;
  785. }
  786. STATIC int
  787. xfs_ioc_fsgetxattr(
  788. xfs_inode_t *ip,
  789. int attr,
  790. void __user *arg)
  791. {
  792. struct fsxattr fa;
  793. memset(&fa, 0, sizeof(struct fsxattr));
  794. xfs_ilock(ip, XFS_ILOCK_SHARED);
  795. fa.fsx_xflags = xfs_ip2xflags(ip);
  796. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  797. fa.fsx_projid = xfs_get_projid(ip);
  798. if (attr) {
  799. if (ip->i_afp) {
  800. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  801. fa.fsx_nextents = ip->i_afp->if_bytes /
  802. sizeof(xfs_bmbt_rec_t);
  803. else
  804. fa.fsx_nextents = ip->i_d.di_anextents;
  805. } else
  806. fa.fsx_nextents = 0;
  807. } else {
  808. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  809. fa.fsx_nextents = ip->i_df.if_bytes /
  810. sizeof(xfs_bmbt_rec_t);
  811. else
  812. fa.fsx_nextents = ip->i_d.di_nextents;
  813. }
  814. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  815. if (copy_to_user(arg, &fa, sizeof(fa)))
  816. return -EFAULT;
  817. return 0;
  818. }
  819. STATIC void
  820. xfs_set_diflags(
  821. struct xfs_inode *ip,
  822. unsigned int xflags)
  823. {
  824. unsigned int di_flags;
  825. /* can't set PREALLOC this way, just preserve it */
  826. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  827. if (xflags & XFS_XFLAG_IMMUTABLE)
  828. di_flags |= XFS_DIFLAG_IMMUTABLE;
  829. if (xflags & XFS_XFLAG_APPEND)
  830. di_flags |= XFS_DIFLAG_APPEND;
  831. if (xflags & XFS_XFLAG_SYNC)
  832. di_flags |= XFS_DIFLAG_SYNC;
  833. if (xflags & XFS_XFLAG_NOATIME)
  834. di_flags |= XFS_DIFLAG_NOATIME;
  835. if (xflags & XFS_XFLAG_NODUMP)
  836. di_flags |= XFS_DIFLAG_NODUMP;
  837. if (xflags & XFS_XFLAG_PROJINHERIT)
  838. di_flags |= XFS_DIFLAG_PROJINHERIT;
  839. if (xflags & XFS_XFLAG_NODEFRAG)
  840. di_flags |= XFS_DIFLAG_NODEFRAG;
  841. if (xflags & XFS_XFLAG_FILESTREAM)
  842. di_flags |= XFS_DIFLAG_FILESTREAM;
  843. if (S_ISDIR(ip->i_d.di_mode)) {
  844. if (xflags & XFS_XFLAG_RTINHERIT)
  845. di_flags |= XFS_DIFLAG_RTINHERIT;
  846. if (xflags & XFS_XFLAG_NOSYMLINKS)
  847. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  848. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  849. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  850. } else if (S_ISREG(ip->i_d.di_mode)) {
  851. if (xflags & XFS_XFLAG_REALTIME)
  852. di_flags |= XFS_DIFLAG_REALTIME;
  853. if (xflags & XFS_XFLAG_EXTSIZE)
  854. di_flags |= XFS_DIFLAG_EXTSIZE;
  855. }
  856. ip->i_d.di_flags = di_flags;
  857. }
  858. STATIC void
  859. xfs_diflags_to_linux(
  860. struct xfs_inode *ip)
  861. {
  862. struct inode *inode = VFS_I(ip);
  863. unsigned int xflags = xfs_ip2xflags(ip);
  864. if (xflags & XFS_XFLAG_IMMUTABLE)
  865. inode->i_flags |= S_IMMUTABLE;
  866. else
  867. inode->i_flags &= ~S_IMMUTABLE;
  868. if (xflags & XFS_XFLAG_APPEND)
  869. inode->i_flags |= S_APPEND;
  870. else
  871. inode->i_flags &= ~S_APPEND;
  872. if (xflags & XFS_XFLAG_SYNC)
  873. inode->i_flags |= S_SYNC;
  874. else
  875. inode->i_flags &= ~S_SYNC;
  876. if (xflags & XFS_XFLAG_NOATIME)
  877. inode->i_flags |= S_NOATIME;
  878. else
  879. inode->i_flags &= ~S_NOATIME;
  880. }
  881. #define FSX_PROJID 1
  882. #define FSX_EXTSIZE 2
  883. #define FSX_XFLAGS 4
  884. #define FSX_NONBLOCK 8
  885. STATIC int
  886. xfs_ioctl_setattr(
  887. xfs_inode_t *ip,
  888. struct fsxattr *fa,
  889. int mask)
  890. {
  891. struct xfs_mount *mp = ip->i_mount;
  892. struct xfs_trans *tp;
  893. unsigned int lock_flags = 0;
  894. struct xfs_dquot *udqp = NULL;
  895. struct xfs_dquot *pdqp = NULL;
  896. struct xfs_dquot *olddquot = NULL;
  897. int code;
  898. trace_xfs_ioctl_setattr(ip);
  899. if (mp->m_flags & XFS_MOUNT_RDONLY)
  900. return XFS_ERROR(EROFS);
  901. if (XFS_FORCED_SHUTDOWN(mp))
  902. return XFS_ERROR(EIO);
  903. /*
  904. * Disallow 32bit project ids when projid32bit feature is not enabled.
  905. */
  906. if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
  907. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  908. return XFS_ERROR(EINVAL);
  909. /*
  910. * If disk quotas is on, we make sure that the dquots do exist on disk,
  911. * before we start any other transactions. Trying to do this later
  912. * is messy. We don't care to take a readlock to look at the ids
  913. * in inode here, because we can't hold it across the trans_reserve.
  914. * If the IDs do change before we take the ilock, we're covered
  915. * because the i_*dquot fields will get updated anyway.
  916. */
  917. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  918. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  919. ip->i_d.di_gid, fa->fsx_projid,
  920. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  921. if (code)
  922. return code;
  923. }
  924. /*
  925. * For the other attributes, we acquire the inode lock and
  926. * first do an error checking pass.
  927. */
  928. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  929. code = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  930. if (code)
  931. goto error_return;
  932. lock_flags = XFS_ILOCK_EXCL;
  933. xfs_ilock(ip, lock_flags);
  934. /*
  935. * CAP_FOWNER overrides the following restrictions:
  936. *
  937. * The user ID of the calling process must be equal
  938. * to the file owner ID, except in cases where the
  939. * CAP_FSETID capability is applicable.
  940. */
  941. if (!inode_owner_or_capable(VFS_I(ip))) {
  942. code = XFS_ERROR(EPERM);
  943. goto error_return;
  944. }
  945. /*
  946. * Do a quota reservation only if projid is actually going to change.
  947. * Only allow changing of projid from init_user_ns since it is a
  948. * non user namespace aware identifier.
  949. */
  950. if (mask & FSX_PROJID) {
  951. if (current_user_ns() != &init_user_ns) {
  952. code = XFS_ERROR(EINVAL);
  953. goto error_return;
  954. }
  955. if (XFS_IS_QUOTA_RUNNING(mp) &&
  956. XFS_IS_PQUOTA_ON(mp) &&
  957. xfs_get_projid(ip) != fa->fsx_projid) {
  958. ASSERT(tp);
  959. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
  960. pdqp, capable(CAP_FOWNER) ?
  961. XFS_QMOPT_FORCE_RES : 0);
  962. if (code) /* out of quota */
  963. goto error_return;
  964. }
  965. }
  966. if (mask & FSX_EXTSIZE) {
  967. /*
  968. * Can't change extent size if any extents are allocated.
  969. */
  970. if (ip->i_d.di_nextents &&
  971. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  972. fa->fsx_extsize)) {
  973. code = XFS_ERROR(EINVAL); /* EFBIG? */
  974. goto error_return;
  975. }
  976. /*
  977. * Extent size must be a multiple of the appropriate block
  978. * size, if set at all. It must also be smaller than the
  979. * maximum extent size supported by the filesystem.
  980. *
  981. * Also, for non-realtime files, limit the extent size hint to
  982. * half the size of the AGs in the filesystem so alignment
  983. * doesn't result in extents larger than an AG.
  984. */
  985. if (fa->fsx_extsize != 0) {
  986. xfs_extlen_t size;
  987. xfs_fsblock_t extsize_fsb;
  988. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  989. if (extsize_fsb > MAXEXTLEN) {
  990. code = XFS_ERROR(EINVAL);
  991. goto error_return;
  992. }
  993. if (XFS_IS_REALTIME_INODE(ip) ||
  994. ((mask & FSX_XFLAGS) &&
  995. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  996. size = mp->m_sb.sb_rextsize <<
  997. mp->m_sb.sb_blocklog;
  998. } else {
  999. size = mp->m_sb.sb_blocksize;
  1000. if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
  1001. code = XFS_ERROR(EINVAL);
  1002. goto error_return;
  1003. }
  1004. }
  1005. if (fa->fsx_extsize % size) {
  1006. code = XFS_ERROR(EINVAL);
  1007. goto error_return;
  1008. }
  1009. }
  1010. }
  1011. if (mask & FSX_XFLAGS) {
  1012. /*
  1013. * Can't change realtime flag if any extents are allocated.
  1014. */
  1015. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  1016. (XFS_IS_REALTIME_INODE(ip)) !=
  1017. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  1018. code = XFS_ERROR(EINVAL); /* EFBIG? */
  1019. goto error_return;
  1020. }
  1021. /*
  1022. * If realtime flag is set then must have realtime data.
  1023. */
  1024. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  1025. if ((mp->m_sb.sb_rblocks == 0) ||
  1026. (mp->m_sb.sb_rextsize == 0) ||
  1027. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  1028. code = XFS_ERROR(EINVAL);
  1029. goto error_return;
  1030. }
  1031. }
  1032. /*
  1033. * Can't modify an immutable/append-only file unless
  1034. * we have appropriate permission.
  1035. */
  1036. if ((ip->i_d.di_flags &
  1037. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  1038. (fa->fsx_xflags &
  1039. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  1040. !capable(CAP_LINUX_IMMUTABLE)) {
  1041. code = XFS_ERROR(EPERM);
  1042. goto error_return;
  1043. }
  1044. }
  1045. xfs_trans_ijoin(tp, ip, 0);
  1046. /*
  1047. * Change file ownership. Must be the owner or privileged.
  1048. */
  1049. if (mask & FSX_PROJID) {
  1050. /*
  1051. * CAP_FSETID overrides the following restrictions:
  1052. *
  1053. * The set-user-ID and set-group-ID bits of a file will be
  1054. * cleared upon successful return from chown()
  1055. */
  1056. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  1057. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1058. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  1059. /*
  1060. * Change the ownerships and register quota modifications
  1061. * in the transaction.
  1062. */
  1063. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1064. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1065. olddquot = xfs_qm_vop_chown(tp, ip,
  1066. &ip->i_pdquot, pdqp);
  1067. }
  1068. ASSERT(ip->i_d.di_version > 1);
  1069. xfs_set_projid(ip, fa->fsx_projid);
  1070. }
  1071. }
  1072. if (mask & FSX_EXTSIZE)
  1073. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1074. if (mask & FSX_XFLAGS) {
  1075. xfs_set_diflags(ip, fa->fsx_xflags);
  1076. xfs_diflags_to_linux(ip);
  1077. }
  1078. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  1079. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1080. XFS_STATS_INC(xs_ig_attrchg);
  1081. /*
  1082. * If this is a synchronous mount, make sure that the
  1083. * transaction goes to disk before returning to the user.
  1084. * This is slightly sub-optimal in that truncates require
  1085. * two sync transactions instead of one for wsync filesystems.
  1086. * One for the truncate and one for the timestamps since we
  1087. * don't want to change the timestamps unless we're sure the
  1088. * truncate worked. Truncates are less than 1% of the laddis
  1089. * mix so this probably isn't worth the trouble to optimize.
  1090. */
  1091. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1092. xfs_trans_set_sync(tp);
  1093. code = xfs_trans_commit(tp, 0);
  1094. xfs_iunlock(ip, lock_flags);
  1095. /*
  1096. * Release any dquot(s) the inode had kept before chown.
  1097. */
  1098. xfs_qm_dqrele(olddquot);
  1099. xfs_qm_dqrele(udqp);
  1100. xfs_qm_dqrele(pdqp);
  1101. return code;
  1102. error_return:
  1103. xfs_qm_dqrele(udqp);
  1104. xfs_qm_dqrele(pdqp);
  1105. xfs_trans_cancel(tp, 0);
  1106. if (lock_flags)
  1107. xfs_iunlock(ip, lock_flags);
  1108. return code;
  1109. }
  1110. STATIC int
  1111. xfs_ioc_fssetxattr(
  1112. xfs_inode_t *ip,
  1113. struct file *filp,
  1114. void __user *arg)
  1115. {
  1116. struct fsxattr fa;
  1117. unsigned int mask;
  1118. int error;
  1119. if (copy_from_user(&fa, arg, sizeof(fa)))
  1120. return -EFAULT;
  1121. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  1122. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1123. mask |= FSX_NONBLOCK;
  1124. error = mnt_want_write_file(filp);
  1125. if (error)
  1126. return error;
  1127. error = xfs_ioctl_setattr(ip, &fa, mask);
  1128. mnt_drop_write_file(filp);
  1129. return -error;
  1130. }
  1131. STATIC int
  1132. xfs_ioc_getxflags(
  1133. xfs_inode_t *ip,
  1134. void __user *arg)
  1135. {
  1136. unsigned int flags;
  1137. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1138. if (copy_to_user(arg, &flags, sizeof(flags)))
  1139. return -EFAULT;
  1140. return 0;
  1141. }
  1142. STATIC int
  1143. xfs_ioc_setxflags(
  1144. xfs_inode_t *ip,
  1145. struct file *filp,
  1146. void __user *arg)
  1147. {
  1148. struct fsxattr fa;
  1149. unsigned int flags;
  1150. unsigned int mask;
  1151. int error;
  1152. if (copy_from_user(&flags, arg, sizeof(flags)))
  1153. return -EFAULT;
  1154. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1155. FS_NOATIME_FL | FS_NODUMP_FL | \
  1156. FS_SYNC_FL))
  1157. return -EOPNOTSUPP;
  1158. mask = FSX_XFLAGS;
  1159. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1160. mask |= FSX_NONBLOCK;
  1161. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1162. error = mnt_want_write_file(filp);
  1163. if (error)
  1164. return error;
  1165. error = xfs_ioctl_setattr(ip, &fa, mask);
  1166. mnt_drop_write_file(filp);
  1167. return -error;
  1168. }
  1169. STATIC int
  1170. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1171. {
  1172. struct getbmap __user *base = *ap;
  1173. /* copy only getbmap portion (not getbmapx) */
  1174. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1175. return XFS_ERROR(EFAULT);
  1176. *ap += sizeof(struct getbmap);
  1177. return 0;
  1178. }
  1179. STATIC int
  1180. xfs_ioc_getbmap(
  1181. struct xfs_inode *ip,
  1182. int ioflags,
  1183. unsigned int cmd,
  1184. void __user *arg)
  1185. {
  1186. struct getbmapx bmx;
  1187. int error;
  1188. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1189. return -XFS_ERROR(EFAULT);
  1190. if (bmx.bmv_count < 2)
  1191. return -XFS_ERROR(EINVAL);
  1192. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1193. if (ioflags & IO_INVIS)
  1194. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1195. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1196. (struct getbmap *)arg+1);
  1197. if (error)
  1198. return -error;
  1199. /* copy back header - only size of getbmap */
  1200. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1201. return -XFS_ERROR(EFAULT);
  1202. return 0;
  1203. }
  1204. STATIC int
  1205. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1206. {
  1207. struct getbmapx __user *base = *ap;
  1208. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1209. return XFS_ERROR(EFAULT);
  1210. *ap += sizeof(struct getbmapx);
  1211. return 0;
  1212. }
  1213. STATIC int
  1214. xfs_ioc_getbmapx(
  1215. struct xfs_inode *ip,
  1216. void __user *arg)
  1217. {
  1218. struct getbmapx bmx;
  1219. int error;
  1220. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1221. return -XFS_ERROR(EFAULT);
  1222. if (bmx.bmv_count < 2)
  1223. return -XFS_ERROR(EINVAL);
  1224. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1225. return -XFS_ERROR(EINVAL);
  1226. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1227. (struct getbmapx *)arg+1);
  1228. if (error)
  1229. return -error;
  1230. /* copy back header */
  1231. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1232. return -XFS_ERROR(EFAULT);
  1233. return 0;
  1234. }
  1235. int
  1236. xfs_ioc_swapext(
  1237. xfs_swapext_t *sxp)
  1238. {
  1239. xfs_inode_t *ip, *tip;
  1240. struct fd f, tmp;
  1241. int error = 0;
  1242. /* Pull information for the target fd */
  1243. f = fdget((int)sxp->sx_fdtarget);
  1244. if (!f.file) {
  1245. error = XFS_ERROR(EINVAL);
  1246. goto out;
  1247. }
  1248. if (!(f.file->f_mode & FMODE_WRITE) ||
  1249. !(f.file->f_mode & FMODE_READ) ||
  1250. (f.file->f_flags & O_APPEND)) {
  1251. error = XFS_ERROR(EBADF);
  1252. goto out_put_file;
  1253. }
  1254. tmp = fdget((int)sxp->sx_fdtmp);
  1255. if (!tmp.file) {
  1256. error = XFS_ERROR(EINVAL);
  1257. goto out_put_file;
  1258. }
  1259. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1260. !(tmp.file->f_mode & FMODE_READ) ||
  1261. (tmp.file->f_flags & O_APPEND)) {
  1262. error = XFS_ERROR(EBADF);
  1263. goto out_put_tmp_file;
  1264. }
  1265. if (IS_SWAPFILE(file_inode(f.file)) ||
  1266. IS_SWAPFILE(file_inode(tmp.file))) {
  1267. error = XFS_ERROR(EINVAL);
  1268. goto out_put_tmp_file;
  1269. }
  1270. ip = XFS_I(file_inode(f.file));
  1271. tip = XFS_I(file_inode(tmp.file));
  1272. if (ip->i_mount != tip->i_mount) {
  1273. error = XFS_ERROR(EINVAL);
  1274. goto out_put_tmp_file;
  1275. }
  1276. if (ip->i_ino == tip->i_ino) {
  1277. error = XFS_ERROR(EINVAL);
  1278. goto out_put_tmp_file;
  1279. }
  1280. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1281. error = XFS_ERROR(EIO);
  1282. goto out_put_tmp_file;
  1283. }
  1284. error = xfs_swap_extents(ip, tip, sxp);
  1285. out_put_tmp_file:
  1286. fdput(tmp);
  1287. out_put_file:
  1288. fdput(f);
  1289. out:
  1290. return error;
  1291. }
  1292. /*
  1293. * Note: some of the ioctl's return positive numbers as a
  1294. * byte count indicating success, such as readlink_by_handle.
  1295. * So we don't "sign flip" like most other routines. This means
  1296. * true errors need to be returned as a negative value.
  1297. */
  1298. long
  1299. xfs_file_ioctl(
  1300. struct file *filp,
  1301. unsigned int cmd,
  1302. unsigned long p)
  1303. {
  1304. struct inode *inode = file_inode(filp);
  1305. struct xfs_inode *ip = XFS_I(inode);
  1306. struct xfs_mount *mp = ip->i_mount;
  1307. void __user *arg = (void __user *)p;
  1308. int ioflags = 0;
  1309. int error;
  1310. if (filp->f_mode & FMODE_NOCMTIME)
  1311. ioflags |= IO_INVIS;
  1312. trace_xfs_file_ioctl(ip);
  1313. switch (cmd) {
  1314. case FITRIM:
  1315. return xfs_ioc_trim(mp, arg);
  1316. case XFS_IOC_ALLOCSP:
  1317. case XFS_IOC_FREESP:
  1318. case XFS_IOC_RESVSP:
  1319. case XFS_IOC_UNRESVSP:
  1320. case XFS_IOC_ALLOCSP64:
  1321. case XFS_IOC_FREESP64:
  1322. case XFS_IOC_RESVSP64:
  1323. case XFS_IOC_UNRESVSP64:
  1324. case XFS_IOC_ZERO_RANGE: {
  1325. xfs_flock64_t bf;
  1326. if (copy_from_user(&bf, arg, sizeof(bf)))
  1327. return -XFS_ERROR(EFAULT);
  1328. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1329. }
  1330. case XFS_IOC_DIOINFO: {
  1331. struct dioattr da;
  1332. xfs_buftarg_t *target =
  1333. XFS_IS_REALTIME_INODE(ip) ?
  1334. mp->m_rtdev_targp : mp->m_ddev_targp;
  1335. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1336. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1337. if (copy_to_user(arg, &da, sizeof(da)))
  1338. return -XFS_ERROR(EFAULT);
  1339. return 0;
  1340. }
  1341. case XFS_IOC_FSBULKSTAT_SINGLE:
  1342. case XFS_IOC_FSBULKSTAT:
  1343. case XFS_IOC_FSINUMBERS:
  1344. return xfs_ioc_bulkstat(mp, cmd, arg);
  1345. case XFS_IOC_FSGEOMETRY_V1:
  1346. return xfs_ioc_fsgeometry_v1(mp, arg);
  1347. case XFS_IOC_FSGEOMETRY:
  1348. return xfs_ioc_fsgeometry(mp, arg);
  1349. case XFS_IOC_GETVERSION:
  1350. return put_user(inode->i_generation, (int __user *)arg);
  1351. case XFS_IOC_FSGETXATTR:
  1352. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1353. case XFS_IOC_FSGETXATTRA:
  1354. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1355. case XFS_IOC_FSSETXATTR:
  1356. return xfs_ioc_fssetxattr(ip, filp, arg);
  1357. case XFS_IOC_GETXFLAGS:
  1358. return xfs_ioc_getxflags(ip, arg);
  1359. case XFS_IOC_SETXFLAGS:
  1360. return xfs_ioc_setxflags(ip, filp, arg);
  1361. case XFS_IOC_FSSETDM: {
  1362. struct fsdmidata dmi;
  1363. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1364. return -XFS_ERROR(EFAULT);
  1365. error = mnt_want_write_file(filp);
  1366. if (error)
  1367. return error;
  1368. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1369. dmi.fsd_dmstate);
  1370. mnt_drop_write_file(filp);
  1371. return -error;
  1372. }
  1373. case XFS_IOC_GETBMAP:
  1374. case XFS_IOC_GETBMAPA:
  1375. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1376. case XFS_IOC_GETBMAPX:
  1377. return xfs_ioc_getbmapx(ip, arg);
  1378. case XFS_IOC_FD_TO_HANDLE:
  1379. case XFS_IOC_PATH_TO_HANDLE:
  1380. case XFS_IOC_PATH_TO_FSHANDLE: {
  1381. xfs_fsop_handlereq_t hreq;
  1382. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1383. return -XFS_ERROR(EFAULT);
  1384. return xfs_find_handle(cmd, &hreq);
  1385. }
  1386. case XFS_IOC_OPEN_BY_HANDLE: {
  1387. xfs_fsop_handlereq_t hreq;
  1388. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1389. return -XFS_ERROR(EFAULT);
  1390. return xfs_open_by_handle(filp, &hreq);
  1391. }
  1392. case XFS_IOC_FSSETDM_BY_HANDLE:
  1393. return xfs_fssetdm_by_handle(filp, arg);
  1394. case XFS_IOC_READLINK_BY_HANDLE: {
  1395. xfs_fsop_handlereq_t hreq;
  1396. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1397. return -XFS_ERROR(EFAULT);
  1398. return xfs_readlink_by_handle(filp, &hreq);
  1399. }
  1400. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1401. return xfs_attrlist_by_handle(filp, arg);
  1402. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1403. return xfs_attrmulti_by_handle(filp, arg);
  1404. case XFS_IOC_SWAPEXT: {
  1405. struct xfs_swapext sxp;
  1406. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1407. return -XFS_ERROR(EFAULT);
  1408. error = mnt_want_write_file(filp);
  1409. if (error)
  1410. return error;
  1411. error = xfs_ioc_swapext(&sxp);
  1412. mnt_drop_write_file(filp);
  1413. return -error;
  1414. }
  1415. case XFS_IOC_FSCOUNTS: {
  1416. xfs_fsop_counts_t out;
  1417. error = xfs_fs_counts(mp, &out);
  1418. if (error)
  1419. return -error;
  1420. if (copy_to_user(arg, &out, sizeof(out)))
  1421. return -XFS_ERROR(EFAULT);
  1422. return 0;
  1423. }
  1424. case XFS_IOC_SET_RESBLKS: {
  1425. xfs_fsop_resblks_t inout;
  1426. __uint64_t in;
  1427. if (!capable(CAP_SYS_ADMIN))
  1428. return -EPERM;
  1429. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1430. return -XFS_ERROR(EROFS);
  1431. if (copy_from_user(&inout, arg, sizeof(inout)))
  1432. return -XFS_ERROR(EFAULT);
  1433. error = mnt_want_write_file(filp);
  1434. if (error)
  1435. return error;
  1436. /* input parameter is passed in resblks field of structure */
  1437. in = inout.resblks;
  1438. error = xfs_reserve_blocks(mp, &in, &inout);
  1439. mnt_drop_write_file(filp);
  1440. if (error)
  1441. return -error;
  1442. if (copy_to_user(arg, &inout, sizeof(inout)))
  1443. return -XFS_ERROR(EFAULT);
  1444. return 0;
  1445. }
  1446. case XFS_IOC_GET_RESBLKS: {
  1447. xfs_fsop_resblks_t out;
  1448. if (!capable(CAP_SYS_ADMIN))
  1449. return -EPERM;
  1450. error = xfs_reserve_blocks(mp, NULL, &out);
  1451. if (error)
  1452. return -error;
  1453. if (copy_to_user(arg, &out, sizeof(out)))
  1454. return -XFS_ERROR(EFAULT);
  1455. return 0;
  1456. }
  1457. case XFS_IOC_FSGROWFSDATA: {
  1458. xfs_growfs_data_t in;
  1459. if (copy_from_user(&in, arg, sizeof(in)))
  1460. return -XFS_ERROR(EFAULT);
  1461. error = mnt_want_write_file(filp);
  1462. if (error)
  1463. return error;
  1464. error = xfs_growfs_data(mp, &in);
  1465. mnt_drop_write_file(filp);
  1466. return -error;
  1467. }
  1468. case XFS_IOC_FSGROWFSLOG: {
  1469. xfs_growfs_log_t in;
  1470. if (copy_from_user(&in, arg, sizeof(in)))
  1471. return -XFS_ERROR(EFAULT);
  1472. error = mnt_want_write_file(filp);
  1473. if (error)
  1474. return error;
  1475. error = xfs_growfs_log(mp, &in);
  1476. mnt_drop_write_file(filp);
  1477. return -error;
  1478. }
  1479. case XFS_IOC_FSGROWFSRT: {
  1480. xfs_growfs_rt_t in;
  1481. if (copy_from_user(&in, arg, sizeof(in)))
  1482. return -XFS_ERROR(EFAULT);
  1483. error = mnt_want_write_file(filp);
  1484. if (error)
  1485. return error;
  1486. error = xfs_growfs_rt(mp, &in);
  1487. mnt_drop_write_file(filp);
  1488. return -error;
  1489. }
  1490. case XFS_IOC_GOINGDOWN: {
  1491. __uint32_t in;
  1492. if (!capable(CAP_SYS_ADMIN))
  1493. return -EPERM;
  1494. if (get_user(in, (__uint32_t __user *)arg))
  1495. return -XFS_ERROR(EFAULT);
  1496. error = xfs_fs_goingdown(mp, in);
  1497. return -error;
  1498. }
  1499. case XFS_IOC_ERROR_INJECTION: {
  1500. xfs_error_injection_t in;
  1501. if (!capable(CAP_SYS_ADMIN))
  1502. return -EPERM;
  1503. if (copy_from_user(&in, arg, sizeof(in)))
  1504. return -XFS_ERROR(EFAULT);
  1505. error = xfs_errortag_add(in.errtag, mp);
  1506. return -error;
  1507. }
  1508. case XFS_IOC_ERROR_CLEARALL:
  1509. if (!capable(CAP_SYS_ADMIN))
  1510. return -EPERM;
  1511. error = xfs_errortag_clearall(mp, 1);
  1512. return -error;
  1513. case XFS_IOC_FREE_EOFBLOCKS: {
  1514. struct xfs_fs_eofblocks eofb;
  1515. struct xfs_eofblocks keofb;
  1516. if (!capable(CAP_SYS_ADMIN))
  1517. return -EPERM;
  1518. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1519. return -XFS_ERROR(EROFS);
  1520. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1521. return -XFS_ERROR(EFAULT);
  1522. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1523. if (error)
  1524. return -error;
  1525. return -xfs_icache_free_eofblocks(mp, &keofb);
  1526. }
  1527. default:
  1528. return -ENOTTY;
  1529. }
  1530. }