xfs_ioctl.c 34 KB

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