xfs_ioctl.c 38 KB

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