xfs_ioctl.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_ioctl.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_attr.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_buf_item.h"
  46. #include "xfs_utils.h"
  47. #include "xfs_dfrag.h"
  48. #include "xfs_fsops.h"
  49. #include "xfs_vnodeops.h"
  50. #include "xfs_quota.h"
  51. #include "xfs_inode_item.h"
  52. #include "xfs_export.h"
  53. #include "xfs_trace.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/slab.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. unsigned char *name,
  389. unsigned char __user *ubuf,
  390. __uint32_t *len,
  391. __uint32_t flags)
  392. {
  393. unsigned char *kbuf;
  394. int error = EFAULT;
  395. if (*len > XATTR_SIZE_MAX)
  396. return EINVAL;
  397. kbuf = 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. unsigned char *name,
  413. const unsigned char __user *ubuf,
  414. __uint32_t len,
  415. __uint32_t flags)
  416. {
  417. unsigned char *kbuf;
  418. int error = EFAULT;
  419. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  420. return EPERM;
  421. if (len > XATTR_SIZE_MAX)
  422. return EINVAL;
  423. kbuf = memdup_user(ubuf, len);
  424. if (IS_ERR(kbuf))
  425. return PTR_ERR(kbuf);
  426. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  427. return error;
  428. }
  429. int
  430. xfs_attrmulti_attr_remove(
  431. struct inode *inode,
  432. unsigned char *name,
  433. __uint32_t flags)
  434. {
  435. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  436. return EPERM;
  437. return xfs_attr_remove(XFS_I(inode), name, flags);
  438. }
  439. STATIC int
  440. xfs_attrmulti_by_handle(
  441. struct file *parfilp,
  442. void __user *arg)
  443. {
  444. int error;
  445. xfs_attr_multiop_t *ops;
  446. xfs_fsop_attrmulti_handlereq_t am_hreq;
  447. struct dentry *dentry;
  448. unsigned int i, size;
  449. unsigned char *attr_name;
  450. if (!capable(CAP_SYS_ADMIN))
  451. return -XFS_ERROR(EPERM);
  452. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  453. return -XFS_ERROR(EFAULT);
  454. /* overflow check */
  455. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  456. return -E2BIG;
  457. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  458. if (IS_ERR(dentry))
  459. return PTR_ERR(dentry);
  460. error = E2BIG;
  461. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  462. if (!size || size > 16 * PAGE_SIZE)
  463. goto out_dput;
  464. ops = memdup_user(am_hreq.ops, size);
  465. if (IS_ERR(ops)) {
  466. error = PTR_ERR(ops);
  467. goto out_dput;
  468. }
  469. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  470. if (!attr_name)
  471. goto out_kfree_ops;
  472. error = 0;
  473. for (i = 0; i < am_hreq.opcount; i++) {
  474. ops[i].am_error = strncpy_from_user((char *)attr_name,
  475. ops[i].am_attrname, MAXNAMELEN);
  476. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  477. error = -ERANGE;
  478. if (ops[i].am_error < 0)
  479. break;
  480. switch (ops[i].am_opcode) {
  481. case ATTR_OP_GET:
  482. ops[i].am_error = xfs_attrmulti_attr_get(
  483. dentry->d_inode, attr_name,
  484. ops[i].am_attrvalue, &ops[i].am_length,
  485. ops[i].am_flags);
  486. break;
  487. case ATTR_OP_SET:
  488. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  489. if (ops[i].am_error)
  490. break;
  491. ops[i].am_error = xfs_attrmulti_attr_set(
  492. dentry->d_inode, attr_name,
  493. ops[i].am_attrvalue, ops[i].am_length,
  494. ops[i].am_flags);
  495. mnt_drop_write(parfilp->f_path.mnt);
  496. break;
  497. case ATTR_OP_REMOVE:
  498. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  499. if (ops[i].am_error)
  500. break;
  501. ops[i].am_error = xfs_attrmulti_attr_remove(
  502. dentry->d_inode, attr_name,
  503. ops[i].am_flags);
  504. mnt_drop_write(parfilp->f_path.mnt);
  505. break;
  506. default:
  507. ops[i].am_error = EINVAL;
  508. }
  509. }
  510. if (copy_to_user(am_hreq.ops, ops, size))
  511. error = XFS_ERROR(EFAULT);
  512. kfree(attr_name);
  513. out_kfree_ops:
  514. kfree(ops);
  515. out_dput:
  516. dput(dentry);
  517. return -error;
  518. }
  519. int
  520. xfs_ioc_space(
  521. struct xfs_inode *ip,
  522. struct inode *inode,
  523. struct file *filp,
  524. int ioflags,
  525. unsigned int cmd,
  526. xfs_flock64_t *bf)
  527. {
  528. int attr_flags = 0;
  529. int error;
  530. /*
  531. * Only allow the sys admin to reserve space unless
  532. * unwritten extents are enabled.
  533. */
  534. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  535. !capable(CAP_SYS_ADMIN))
  536. return -XFS_ERROR(EPERM);
  537. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  538. return -XFS_ERROR(EPERM);
  539. if (!(filp->f_mode & FMODE_WRITE))
  540. return -XFS_ERROR(EBADF);
  541. if (!S_ISREG(inode->i_mode))
  542. return -XFS_ERROR(EINVAL);
  543. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  544. attr_flags |= XFS_ATTR_NONBLOCK;
  545. if (ioflags & IO_INVIS)
  546. attr_flags |= XFS_ATTR_DMI;
  547. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  548. return -error;
  549. }
  550. STATIC int
  551. xfs_ioc_bulkstat(
  552. xfs_mount_t *mp,
  553. unsigned int cmd,
  554. void __user *arg)
  555. {
  556. xfs_fsop_bulkreq_t bulkreq;
  557. int count; /* # of records returned */
  558. xfs_ino_t inlast; /* last inode number */
  559. int done;
  560. int error;
  561. /* done = 1 if there are more stats to get and if bulkstat */
  562. /* should be called again (unused here, but used in dmapi) */
  563. if (!capable(CAP_SYS_ADMIN))
  564. return -EPERM;
  565. if (XFS_FORCED_SHUTDOWN(mp))
  566. return -XFS_ERROR(EIO);
  567. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  568. return -XFS_ERROR(EFAULT);
  569. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  570. return -XFS_ERROR(EFAULT);
  571. if ((count = bulkreq.icount) <= 0)
  572. return -XFS_ERROR(EINVAL);
  573. if (bulkreq.ubuffer == NULL)
  574. return -XFS_ERROR(EINVAL);
  575. if (cmd == XFS_IOC_FSINUMBERS)
  576. error = xfs_inumbers(mp, &inlast, &count,
  577. bulkreq.ubuffer, xfs_inumbers_fmt);
  578. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  579. error = xfs_bulkstat_single(mp, &inlast,
  580. bulkreq.ubuffer, &done);
  581. else /* XFS_IOC_FSBULKSTAT */
  582. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  583. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  584. &done);
  585. if (error)
  586. return -error;
  587. if (bulkreq.ocount != NULL) {
  588. if (copy_to_user(bulkreq.lastip, &inlast,
  589. sizeof(xfs_ino_t)))
  590. return -XFS_ERROR(EFAULT);
  591. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  592. return -XFS_ERROR(EFAULT);
  593. }
  594. return 0;
  595. }
  596. STATIC int
  597. xfs_ioc_fsgeometry_v1(
  598. xfs_mount_t *mp,
  599. void __user *arg)
  600. {
  601. xfs_fsop_geom_v1_t fsgeo;
  602. int error;
  603. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  604. if (error)
  605. return -error;
  606. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  607. return -XFS_ERROR(EFAULT);
  608. return 0;
  609. }
  610. STATIC int
  611. xfs_ioc_fsgeometry(
  612. xfs_mount_t *mp,
  613. void __user *arg)
  614. {
  615. xfs_fsop_geom_t fsgeo;
  616. int error;
  617. error = xfs_fs_geometry(mp, &fsgeo, 4);
  618. if (error)
  619. return -error;
  620. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  621. return -XFS_ERROR(EFAULT);
  622. return 0;
  623. }
  624. /*
  625. * Linux extended inode flags interface.
  626. */
  627. STATIC unsigned int
  628. xfs_merge_ioc_xflags(
  629. unsigned int flags,
  630. unsigned int start)
  631. {
  632. unsigned int xflags = start;
  633. if (flags & FS_IMMUTABLE_FL)
  634. xflags |= XFS_XFLAG_IMMUTABLE;
  635. else
  636. xflags &= ~XFS_XFLAG_IMMUTABLE;
  637. if (flags & FS_APPEND_FL)
  638. xflags |= XFS_XFLAG_APPEND;
  639. else
  640. xflags &= ~XFS_XFLAG_APPEND;
  641. if (flags & FS_SYNC_FL)
  642. xflags |= XFS_XFLAG_SYNC;
  643. else
  644. xflags &= ~XFS_XFLAG_SYNC;
  645. if (flags & FS_NOATIME_FL)
  646. xflags |= XFS_XFLAG_NOATIME;
  647. else
  648. xflags &= ~XFS_XFLAG_NOATIME;
  649. if (flags & FS_NODUMP_FL)
  650. xflags |= XFS_XFLAG_NODUMP;
  651. else
  652. xflags &= ~XFS_XFLAG_NODUMP;
  653. return xflags;
  654. }
  655. STATIC unsigned int
  656. xfs_di2lxflags(
  657. __uint16_t di_flags)
  658. {
  659. unsigned int flags = 0;
  660. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  661. flags |= FS_IMMUTABLE_FL;
  662. if (di_flags & XFS_DIFLAG_APPEND)
  663. flags |= FS_APPEND_FL;
  664. if (di_flags & XFS_DIFLAG_SYNC)
  665. flags |= FS_SYNC_FL;
  666. if (di_flags & XFS_DIFLAG_NOATIME)
  667. flags |= FS_NOATIME_FL;
  668. if (di_flags & XFS_DIFLAG_NODUMP)
  669. flags |= FS_NODUMP_FL;
  670. return flags;
  671. }
  672. STATIC int
  673. xfs_ioc_fsgetxattr(
  674. xfs_inode_t *ip,
  675. int attr,
  676. void __user *arg)
  677. {
  678. struct fsxattr fa;
  679. xfs_ilock(ip, XFS_ILOCK_SHARED);
  680. fa.fsx_xflags = xfs_ip2xflags(ip);
  681. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  682. fa.fsx_projid = ip->i_d.di_projid;
  683. if (attr) {
  684. if (ip->i_afp) {
  685. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  686. fa.fsx_nextents = ip->i_afp->if_bytes /
  687. sizeof(xfs_bmbt_rec_t);
  688. else
  689. fa.fsx_nextents = ip->i_d.di_anextents;
  690. } else
  691. fa.fsx_nextents = 0;
  692. } else {
  693. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  694. fa.fsx_nextents = ip->i_df.if_bytes /
  695. sizeof(xfs_bmbt_rec_t);
  696. else
  697. fa.fsx_nextents = ip->i_d.di_nextents;
  698. }
  699. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  700. if (copy_to_user(arg, &fa, sizeof(fa)))
  701. return -EFAULT;
  702. return 0;
  703. }
  704. STATIC void
  705. xfs_set_diflags(
  706. struct xfs_inode *ip,
  707. unsigned int xflags)
  708. {
  709. unsigned int di_flags;
  710. /* can't set PREALLOC this way, just preserve it */
  711. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  712. if (xflags & XFS_XFLAG_IMMUTABLE)
  713. di_flags |= XFS_DIFLAG_IMMUTABLE;
  714. if (xflags & XFS_XFLAG_APPEND)
  715. di_flags |= XFS_DIFLAG_APPEND;
  716. if (xflags & XFS_XFLAG_SYNC)
  717. di_flags |= XFS_DIFLAG_SYNC;
  718. if (xflags & XFS_XFLAG_NOATIME)
  719. di_flags |= XFS_DIFLAG_NOATIME;
  720. if (xflags & XFS_XFLAG_NODUMP)
  721. di_flags |= XFS_DIFLAG_NODUMP;
  722. if (xflags & XFS_XFLAG_PROJINHERIT)
  723. di_flags |= XFS_DIFLAG_PROJINHERIT;
  724. if (xflags & XFS_XFLAG_NODEFRAG)
  725. di_flags |= XFS_DIFLAG_NODEFRAG;
  726. if (xflags & XFS_XFLAG_FILESTREAM)
  727. di_flags |= XFS_DIFLAG_FILESTREAM;
  728. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  729. if (xflags & XFS_XFLAG_RTINHERIT)
  730. di_flags |= XFS_DIFLAG_RTINHERIT;
  731. if (xflags & XFS_XFLAG_NOSYMLINKS)
  732. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  733. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  734. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  735. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  736. if (xflags & XFS_XFLAG_REALTIME)
  737. di_flags |= XFS_DIFLAG_REALTIME;
  738. if (xflags & XFS_XFLAG_EXTSIZE)
  739. di_flags |= XFS_DIFLAG_EXTSIZE;
  740. }
  741. ip->i_d.di_flags = di_flags;
  742. }
  743. STATIC void
  744. xfs_diflags_to_linux(
  745. struct xfs_inode *ip)
  746. {
  747. struct inode *inode = VFS_I(ip);
  748. unsigned int xflags = xfs_ip2xflags(ip);
  749. if (xflags & XFS_XFLAG_IMMUTABLE)
  750. inode->i_flags |= S_IMMUTABLE;
  751. else
  752. inode->i_flags &= ~S_IMMUTABLE;
  753. if (xflags & XFS_XFLAG_APPEND)
  754. inode->i_flags |= S_APPEND;
  755. else
  756. inode->i_flags &= ~S_APPEND;
  757. if (xflags & XFS_XFLAG_SYNC)
  758. inode->i_flags |= S_SYNC;
  759. else
  760. inode->i_flags &= ~S_SYNC;
  761. if (xflags & XFS_XFLAG_NOATIME)
  762. inode->i_flags |= S_NOATIME;
  763. else
  764. inode->i_flags &= ~S_NOATIME;
  765. }
  766. #define FSX_PROJID 1
  767. #define FSX_EXTSIZE 2
  768. #define FSX_XFLAGS 4
  769. #define FSX_NONBLOCK 8
  770. STATIC int
  771. xfs_ioctl_setattr(
  772. xfs_inode_t *ip,
  773. struct fsxattr *fa,
  774. int mask)
  775. {
  776. struct xfs_mount *mp = ip->i_mount;
  777. struct xfs_trans *tp;
  778. unsigned int lock_flags = 0;
  779. struct xfs_dquot *udqp = NULL;
  780. struct xfs_dquot *gdqp = NULL;
  781. struct xfs_dquot *olddquot = NULL;
  782. int code;
  783. xfs_itrace_entry(ip);
  784. if (mp->m_flags & XFS_MOUNT_RDONLY)
  785. return XFS_ERROR(EROFS);
  786. if (XFS_FORCED_SHUTDOWN(mp))
  787. return XFS_ERROR(EIO);
  788. /*
  789. * If disk quotas is on, we make sure that the dquots do exist on disk,
  790. * before we start any other transactions. Trying to do this later
  791. * is messy. We don't care to take a readlock to look at the ids
  792. * in inode here, because we can't hold it across the trans_reserve.
  793. * If the IDs do change before we take the ilock, we're covered
  794. * because the i_*dquot fields will get updated anyway.
  795. */
  796. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  797. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  798. ip->i_d.di_gid, fa->fsx_projid,
  799. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  800. if (code)
  801. return code;
  802. }
  803. /*
  804. * For the other attributes, we acquire the inode lock and
  805. * first do an error checking pass.
  806. */
  807. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  808. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  809. if (code)
  810. goto error_return;
  811. lock_flags = XFS_ILOCK_EXCL;
  812. xfs_ilock(ip, lock_flags);
  813. /*
  814. * CAP_FOWNER overrides the following restrictions:
  815. *
  816. * The user ID of the calling process must be equal
  817. * to the file owner ID, except in cases where the
  818. * CAP_FSETID capability is applicable.
  819. */
  820. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  821. code = XFS_ERROR(EPERM);
  822. goto error_return;
  823. }
  824. /*
  825. * Do a quota reservation only if projid is actually going to change.
  826. */
  827. if (mask & FSX_PROJID) {
  828. if (XFS_IS_QUOTA_RUNNING(mp) &&
  829. XFS_IS_PQUOTA_ON(mp) &&
  830. ip->i_d.di_projid != fa->fsx_projid) {
  831. ASSERT(tp);
  832. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  833. capable(CAP_FOWNER) ?
  834. XFS_QMOPT_FORCE_RES : 0);
  835. if (code) /* out of quota */
  836. goto error_return;
  837. }
  838. }
  839. if (mask & FSX_EXTSIZE) {
  840. /*
  841. * Can't change extent size if any extents are allocated.
  842. */
  843. if (ip->i_d.di_nextents &&
  844. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  845. fa->fsx_extsize)) {
  846. code = XFS_ERROR(EINVAL); /* EFBIG? */
  847. goto error_return;
  848. }
  849. /*
  850. * Extent size must be a multiple of the appropriate block
  851. * size, if set at all.
  852. */
  853. if (fa->fsx_extsize != 0) {
  854. xfs_extlen_t size;
  855. if (XFS_IS_REALTIME_INODE(ip) ||
  856. ((mask & FSX_XFLAGS) &&
  857. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  858. size = mp->m_sb.sb_rextsize <<
  859. mp->m_sb.sb_blocklog;
  860. } else {
  861. size = mp->m_sb.sb_blocksize;
  862. }
  863. if (fa->fsx_extsize % size) {
  864. code = XFS_ERROR(EINVAL);
  865. goto error_return;
  866. }
  867. }
  868. }
  869. if (mask & FSX_XFLAGS) {
  870. /*
  871. * Can't change realtime flag if any extents are allocated.
  872. */
  873. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  874. (XFS_IS_REALTIME_INODE(ip)) !=
  875. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  876. code = XFS_ERROR(EINVAL); /* EFBIG? */
  877. goto error_return;
  878. }
  879. /*
  880. * If realtime flag is set then must have realtime data.
  881. */
  882. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  883. if ((mp->m_sb.sb_rblocks == 0) ||
  884. (mp->m_sb.sb_rextsize == 0) ||
  885. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  886. code = XFS_ERROR(EINVAL);
  887. goto error_return;
  888. }
  889. }
  890. /*
  891. * Can't modify an immutable/append-only file unless
  892. * we have appropriate permission.
  893. */
  894. if ((ip->i_d.di_flags &
  895. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  896. (fa->fsx_xflags &
  897. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  898. !capable(CAP_LINUX_IMMUTABLE)) {
  899. code = XFS_ERROR(EPERM);
  900. goto error_return;
  901. }
  902. }
  903. xfs_trans_ijoin(tp, ip, lock_flags);
  904. xfs_trans_ihold(tp, ip);
  905. /*
  906. * Change file ownership. Must be the owner or privileged.
  907. */
  908. if (mask & FSX_PROJID) {
  909. /*
  910. * CAP_FSETID overrides the following restrictions:
  911. *
  912. * The set-user-ID and set-group-ID bits of a file will be
  913. * cleared upon successful return from chown()
  914. */
  915. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  916. !capable(CAP_FSETID))
  917. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  918. /*
  919. * Change the ownerships and register quota modifications
  920. * in the transaction.
  921. */
  922. if (ip->i_d.di_projid != fa->fsx_projid) {
  923. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  924. olddquot = xfs_qm_vop_chown(tp, ip,
  925. &ip->i_gdquot, gdqp);
  926. }
  927. ip->i_d.di_projid = fa->fsx_projid;
  928. /*
  929. * We may have to rev the inode as well as
  930. * the superblock version number since projids didn't
  931. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  932. */
  933. if (ip->i_d.di_version == 1)
  934. xfs_bump_ino_vers2(tp, ip);
  935. }
  936. }
  937. if (mask & FSX_EXTSIZE)
  938. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  939. if (mask & FSX_XFLAGS) {
  940. xfs_set_diflags(ip, fa->fsx_xflags);
  941. xfs_diflags_to_linux(ip);
  942. }
  943. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  944. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  945. XFS_STATS_INC(xs_ig_attrchg);
  946. /*
  947. * If this is a synchronous mount, make sure that the
  948. * transaction goes to disk before returning to the user.
  949. * This is slightly sub-optimal in that truncates require
  950. * two sync transactions instead of one for wsync filesystems.
  951. * One for the truncate and one for the timestamps since we
  952. * don't want to change the timestamps unless we're sure the
  953. * truncate worked. Truncates are less than 1% of the laddis
  954. * mix so this probably isn't worth the trouble to optimize.
  955. */
  956. if (mp->m_flags & XFS_MOUNT_WSYNC)
  957. xfs_trans_set_sync(tp);
  958. code = xfs_trans_commit(tp, 0);
  959. xfs_iunlock(ip, lock_flags);
  960. /*
  961. * Release any dquot(s) the inode had kept before chown.
  962. */
  963. xfs_qm_dqrele(olddquot);
  964. xfs_qm_dqrele(udqp);
  965. xfs_qm_dqrele(gdqp);
  966. return code;
  967. error_return:
  968. xfs_qm_dqrele(udqp);
  969. xfs_qm_dqrele(gdqp);
  970. xfs_trans_cancel(tp, 0);
  971. if (lock_flags)
  972. xfs_iunlock(ip, lock_flags);
  973. return code;
  974. }
  975. STATIC int
  976. xfs_ioc_fssetxattr(
  977. xfs_inode_t *ip,
  978. struct file *filp,
  979. void __user *arg)
  980. {
  981. struct fsxattr fa;
  982. unsigned int mask;
  983. if (copy_from_user(&fa, arg, sizeof(fa)))
  984. return -EFAULT;
  985. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  986. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  987. mask |= FSX_NONBLOCK;
  988. return -xfs_ioctl_setattr(ip, &fa, mask);
  989. }
  990. STATIC int
  991. xfs_ioc_getxflags(
  992. xfs_inode_t *ip,
  993. void __user *arg)
  994. {
  995. unsigned int flags;
  996. flags = xfs_di2lxflags(ip->i_d.di_flags);
  997. if (copy_to_user(arg, &flags, sizeof(flags)))
  998. return -EFAULT;
  999. return 0;
  1000. }
  1001. STATIC int
  1002. xfs_ioc_setxflags(
  1003. xfs_inode_t *ip,
  1004. struct file *filp,
  1005. void __user *arg)
  1006. {
  1007. struct fsxattr fa;
  1008. unsigned int flags;
  1009. unsigned int mask;
  1010. if (copy_from_user(&flags, arg, sizeof(flags)))
  1011. return -EFAULT;
  1012. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1013. FS_NOATIME_FL | FS_NODUMP_FL | \
  1014. FS_SYNC_FL))
  1015. return -EOPNOTSUPP;
  1016. mask = FSX_XFLAGS;
  1017. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1018. mask |= FSX_NONBLOCK;
  1019. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1020. return -xfs_ioctl_setattr(ip, &fa, mask);
  1021. }
  1022. STATIC int
  1023. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1024. {
  1025. struct getbmap __user *base = *ap;
  1026. /* copy only getbmap portion (not getbmapx) */
  1027. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1028. return XFS_ERROR(EFAULT);
  1029. *ap += sizeof(struct getbmap);
  1030. return 0;
  1031. }
  1032. STATIC int
  1033. xfs_ioc_getbmap(
  1034. struct xfs_inode *ip,
  1035. int ioflags,
  1036. unsigned int cmd,
  1037. void __user *arg)
  1038. {
  1039. struct getbmapx bmx;
  1040. int error;
  1041. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1042. return -XFS_ERROR(EFAULT);
  1043. if (bmx.bmv_count < 2)
  1044. return -XFS_ERROR(EINVAL);
  1045. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1046. if (ioflags & IO_INVIS)
  1047. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1048. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1049. (struct getbmap *)arg+1);
  1050. if (error)
  1051. return -error;
  1052. /* copy back header - only size of getbmap */
  1053. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1054. return -XFS_ERROR(EFAULT);
  1055. return 0;
  1056. }
  1057. STATIC int
  1058. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1059. {
  1060. struct getbmapx __user *base = *ap;
  1061. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1062. return XFS_ERROR(EFAULT);
  1063. *ap += sizeof(struct getbmapx);
  1064. return 0;
  1065. }
  1066. STATIC int
  1067. xfs_ioc_getbmapx(
  1068. struct xfs_inode *ip,
  1069. void __user *arg)
  1070. {
  1071. struct getbmapx bmx;
  1072. int error;
  1073. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1074. return -XFS_ERROR(EFAULT);
  1075. if (bmx.bmv_count < 2)
  1076. return -XFS_ERROR(EINVAL);
  1077. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1078. return -XFS_ERROR(EINVAL);
  1079. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1080. (struct getbmapx *)arg+1);
  1081. if (error)
  1082. return -error;
  1083. /* copy back header */
  1084. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1085. return -XFS_ERROR(EFAULT);
  1086. return 0;
  1087. }
  1088. /*
  1089. * Note: some of the ioctl's return positive numbers as a
  1090. * byte count indicating success, such as readlink_by_handle.
  1091. * So we don't "sign flip" like most other routines. This means
  1092. * true errors need to be returned as a negative value.
  1093. */
  1094. long
  1095. xfs_file_ioctl(
  1096. struct file *filp,
  1097. unsigned int cmd,
  1098. unsigned long p)
  1099. {
  1100. struct inode *inode = filp->f_path.dentry->d_inode;
  1101. struct xfs_inode *ip = XFS_I(inode);
  1102. struct xfs_mount *mp = ip->i_mount;
  1103. void __user *arg = (void __user *)p;
  1104. int ioflags = 0;
  1105. int error;
  1106. if (filp->f_mode & FMODE_NOCMTIME)
  1107. ioflags |= IO_INVIS;
  1108. xfs_itrace_entry(ip);
  1109. switch (cmd) {
  1110. case XFS_IOC_ALLOCSP:
  1111. case XFS_IOC_FREESP:
  1112. case XFS_IOC_RESVSP:
  1113. case XFS_IOC_UNRESVSP:
  1114. case XFS_IOC_ALLOCSP64:
  1115. case XFS_IOC_FREESP64:
  1116. case XFS_IOC_RESVSP64:
  1117. case XFS_IOC_UNRESVSP64: {
  1118. xfs_flock64_t bf;
  1119. if (copy_from_user(&bf, arg, sizeof(bf)))
  1120. return -XFS_ERROR(EFAULT);
  1121. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1122. }
  1123. case XFS_IOC_DIOINFO: {
  1124. struct dioattr da;
  1125. xfs_buftarg_t *target =
  1126. XFS_IS_REALTIME_INODE(ip) ?
  1127. mp->m_rtdev_targp : mp->m_ddev_targp;
  1128. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1129. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1130. if (copy_to_user(arg, &da, sizeof(da)))
  1131. return -XFS_ERROR(EFAULT);
  1132. return 0;
  1133. }
  1134. case XFS_IOC_FSBULKSTAT_SINGLE:
  1135. case XFS_IOC_FSBULKSTAT:
  1136. case XFS_IOC_FSINUMBERS:
  1137. return xfs_ioc_bulkstat(mp, cmd, arg);
  1138. case XFS_IOC_FSGEOMETRY_V1:
  1139. return xfs_ioc_fsgeometry_v1(mp, arg);
  1140. case XFS_IOC_FSGEOMETRY:
  1141. return xfs_ioc_fsgeometry(mp, arg);
  1142. case XFS_IOC_GETVERSION:
  1143. return put_user(inode->i_generation, (int __user *)arg);
  1144. case XFS_IOC_FSGETXATTR:
  1145. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1146. case XFS_IOC_FSGETXATTRA:
  1147. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1148. case XFS_IOC_FSSETXATTR:
  1149. return xfs_ioc_fssetxattr(ip, filp, arg);
  1150. case XFS_IOC_GETXFLAGS:
  1151. return xfs_ioc_getxflags(ip, arg);
  1152. case XFS_IOC_SETXFLAGS:
  1153. return xfs_ioc_setxflags(ip, filp, arg);
  1154. case XFS_IOC_FSSETDM: {
  1155. struct fsdmidata dmi;
  1156. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1157. return -XFS_ERROR(EFAULT);
  1158. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1159. dmi.fsd_dmstate);
  1160. return -error;
  1161. }
  1162. case XFS_IOC_GETBMAP:
  1163. case XFS_IOC_GETBMAPA:
  1164. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1165. case XFS_IOC_GETBMAPX:
  1166. return xfs_ioc_getbmapx(ip, arg);
  1167. case XFS_IOC_FD_TO_HANDLE:
  1168. case XFS_IOC_PATH_TO_HANDLE:
  1169. case XFS_IOC_PATH_TO_FSHANDLE: {
  1170. xfs_fsop_handlereq_t hreq;
  1171. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1172. return -XFS_ERROR(EFAULT);
  1173. return xfs_find_handle(cmd, &hreq);
  1174. }
  1175. case XFS_IOC_OPEN_BY_HANDLE: {
  1176. xfs_fsop_handlereq_t hreq;
  1177. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1178. return -XFS_ERROR(EFAULT);
  1179. return xfs_open_by_handle(filp, &hreq);
  1180. }
  1181. case XFS_IOC_FSSETDM_BY_HANDLE:
  1182. return xfs_fssetdm_by_handle(filp, arg);
  1183. case XFS_IOC_READLINK_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_readlink_by_handle(filp, &hreq);
  1188. }
  1189. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1190. return xfs_attrlist_by_handle(filp, arg);
  1191. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1192. return xfs_attrmulti_by_handle(filp, arg);
  1193. case XFS_IOC_SWAPEXT: {
  1194. struct xfs_swapext sxp;
  1195. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1196. return -XFS_ERROR(EFAULT);
  1197. error = xfs_swapext(&sxp);
  1198. return -error;
  1199. }
  1200. case XFS_IOC_FSCOUNTS: {
  1201. xfs_fsop_counts_t out;
  1202. error = xfs_fs_counts(mp, &out);
  1203. if (error)
  1204. return -error;
  1205. if (copy_to_user(arg, &out, sizeof(out)))
  1206. return -XFS_ERROR(EFAULT);
  1207. return 0;
  1208. }
  1209. case XFS_IOC_SET_RESBLKS: {
  1210. xfs_fsop_resblks_t inout;
  1211. __uint64_t in;
  1212. if (!capable(CAP_SYS_ADMIN))
  1213. return -EPERM;
  1214. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1215. return -XFS_ERROR(EROFS);
  1216. if (copy_from_user(&inout, arg, sizeof(inout)))
  1217. return -XFS_ERROR(EFAULT);
  1218. /* input parameter is passed in resblks field of structure */
  1219. in = inout.resblks;
  1220. error = xfs_reserve_blocks(mp, &in, &inout);
  1221. if (error)
  1222. return -error;
  1223. if (copy_to_user(arg, &inout, sizeof(inout)))
  1224. return -XFS_ERROR(EFAULT);
  1225. return 0;
  1226. }
  1227. case XFS_IOC_GET_RESBLKS: {
  1228. xfs_fsop_resblks_t out;
  1229. if (!capable(CAP_SYS_ADMIN))
  1230. return -EPERM;
  1231. error = xfs_reserve_blocks(mp, NULL, &out);
  1232. if (error)
  1233. return -error;
  1234. if (copy_to_user(arg, &out, sizeof(out)))
  1235. return -XFS_ERROR(EFAULT);
  1236. return 0;
  1237. }
  1238. case XFS_IOC_FSGROWFSDATA: {
  1239. xfs_growfs_data_t in;
  1240. if (copy_from_user(&in, arg, sizeof(in)))
  1241. return -XFS_ERROR(EFAULT);
  1242. error = xfs_growfs_data(mp, &in);
  1243. return -error;
  1244. }
  1245. case XFS_IOC_FSGROWFSLOG: {
  1246. xfs_growfs_log_t in;
  1247. if (copy_from_user(&in, arg, sizeof(in)))
  1248. return -XFS_ERROR(EFAULT);
  1249. error = xfs_growfs_log(mp, &in);
  1250. return -error;
  1251. }
  1252. case XFS_IOC_FSGROWFSRT: {
  1253. xfs_growfs_rt_t in;
  1254. if (copy_from_user(&in, arg, sizeof(in)))
  1255. return -XFS_ERROR(EFAULT);
  1256. error = xfs_growfs_rt(mp, &in);
  1257. return -error;
  1258. }
  1259. case XFS_IOC_GOINGDOWN: {
  1260. __uint32_t in;
  1261. if (!capable(CAP_SYS_ADMIN))
  1262. return -EPERM;
  1263. if (get_user(in, (__uint32_t __user *)arg))
  1264. return -XFS_ERROR(EFAULT);
  1265. error = xfs_fs_goingdown(mp, in);
  1266. return -error;
  1267. }
  1268. case XFS_IOC_ERROR_INJECTION: {
  1269. xfs_error_injection_t in;
  1270. if (!capable(CAP_SYS_ADMIN))
  1271. return -EPERM;
  1272. if (copy_from_user(&in, arg, sizeof(in)))
  1273. return -XFS_ERROR(EFAULT);
  1274. error = xfs_errortag_add(in.errtag, mp);
  1275. return -error;
  1276. }
  1277. case XFS_IOC_ERROR_CLEARALL:
  1278. if (!capable(CAP_SYS_ADMIN))
  1279. return -EPERM;
  1280. error = xfs_errortag_clearall(mp, 1);
  1281. return -error;
  1282. default:
  1283. return -ENOTTY;
  1284. }
  1285. }