xfs_ioctl.c 35 KB

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