xfs_ioctl.c 36 KB

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