xfs_ioctl.c 34 KB

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