xfs_ioctl.c 34 KB

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