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