xfs_ioctl.c 34 KB

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