xfs_ioctl.c 35 KB

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