xfs_ioctl.c 40 KB

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