xfs_ioctl.c 35 KB

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