xfs_ioctl.c 34 KB

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