xfs_ioctl.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_ioctl.h"
  29. #include "xfs_alloc.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_bmap_util.h"
  36. #include "xfs_fsops.h"
  37. #include "xfs_discard.h"
  38. #include "xfs_quota.h"
  39. #include "xfs_export.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_icache.h"
  42. #include "xfs_symlink.h"
  43. #include "xfs_dinode.h"
  44. #include "xfs_trans.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 fd f = {NULL};
  72. struct path path;
  73. int error;
  74. struct xfs_inode *ip;
  75. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  76. f = fdget(hreq->fd);
  77. if (!f.file)
  78. return -EBADF;
  79. inode = file_inode(f.file);
  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. fdput(f);
  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(file_inode(parfilp)->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_flags(0);
  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. int
  307. xfs_set_dmattrs(
  308. xfs_inode_t *ip,
  309. u_int evmask,
  310. u_int16_t state)
  311. {
  312. xfs_mount_t *mp = ip->i_mount;
  313. xfs_trans_t *tp;
  314. int error;
  315. if (!capable(CAP_SYS_ADMIN))
  316. return XFS_ERROR(EPERM);
  317. if (XFS_FORCED_SHUTDOWN(mp))
  318. return XFS_ERROR(EIO);
  319. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  320. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  321. if (error) {
  322. xfs_trans_cancel(tp, 0);
  323. return error;
  324. }
  325. xfs_ilock(ip, XFS_ILOCK_EXCL);
  326. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  327. ip->i_d.di_dmevmask = evmask;
  328. ip->i_d.di_dmstate = state;
  329. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  330. error = xfs_trans_commit(tp, 0);
  331. return error;
  332. }
  333. STATIC int
  334. xfs_fssetdm_by_handle(
  335. struct file *parfilp,
  336. void __user *arg)
  337. {
  338. int error;
  339. struct fsdmidata fsd;
  340. xfs_fsop_setdm_handlereq_t dmhreq;
  341. struct dentry *dentry;
  342. if (!capable(CAP_MKNOD))
  343. return -XFS_ERROR(EPERM);
  344. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  345. return -XFS_ERROR(EFAULT);
  346. error = mnt_want_write_file(parfilp);
  347. if (error)
  348. return error;
  349. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  350. if (IS_ERR(dentry)) {
  351. mnt_drop_write_file(parfilp);
  352. return PTR_ERR(dentry);
  353. }
  354. if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
  355. error = -XFS_ERROR(EPERM);
  356. goto out;
  357. }
  358. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  359. error = -XFS_ERROR(EFAULT);
  360. goto out;
  361. }
  362. error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
  363. fsd.fsd_dmstate);
  364. out:
  365. mnt_drop_write_file(parfilp);
  366. dput(dentry);
  367. return error;
  368. }
  369. STATIC int
  370. xfs_attrlist_by_handle(
  371. struct file *parfilp,
  372. void __user *arg)
  373. {
  374. int error = -ENOMEM;
  375. attrlist_cursor_kern_t *cursor;
  376. xfs_fsop_attrlist_handlereq_t al_hreq;
  377. struct dentry *dentry;
  378. char *kbuf;
  379. if (!capable(CAP_SYS_ADMIN))
  380. return -XFS_ERROR(EPERM);
  381. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  382. return -XFS_ERROR(EFAULT);
  383. if (al_hreq.buflen > XATTR_LIST_MAX)
  384. return -XFS_ERROR(EINVAL);
  385. /*
  386. * Reject flags, only allow namespaces.
  387. */
  388. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  389. return -XFS_ERROR(EINVAL);
  390. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  391. if (IS_ERR(dentry))
  392. return PTR_ERR(dentry);
  393. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  394. if (!kbuf)
  395. goto out_dput;
  396. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  397. error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
  398. al_hreq.flags, cursor);
  399. if (error)
  400. goto out_kfree;
  401. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  402. error = -EFAULT;
  403. out_kfree:
  404. kmem_free(kbuf);
  405. out_dput:
  406. dput(dentry);
  407. return error;
  408. }
  409. int
  410. xfs_attrmulti_attr_get(
  411. struct inode *inode,
  412. unsigned char *name,
  413. unsigned char __user *ubuf,
  414. __uint32_t *len,
  415. __uint32_t flags)
  416. {
  417. unsigned char *kbuf;
  418. int error = EFAULT;
  419. if (*len > XATTR_SIZE_MAX)
  420. return EINVAL;
  421. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  422. if (!kbuf)
  423. return ENOMEM;
  424. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  425. if (error)
  426. goto out_kfree;
  427. if (copy_to_user(ubuf, kbuf, *len))
  428. error = EFAULT;
  429. out_kfree:
  430. kmem_free(kbuf);
  431. return error;
  432. }
  433. int
  434. xfs_attrmulti_attr_set(
  435. struct inode *inode,
  436. unsigned char *name,
  437. const unsigned char __user *ubuf,
  438. __uint32_t len,
  439. __uint32_t flags)
  440. {
  441. unsigned char *kbuf;
  442. int error = EFAULT;
  443. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  444. return EPERM;
  445. if (len > XATTR_SIZE_MAX)
  446. return EINVAL;
  447. kbuf = memdup_user(ubuf, len);
  448. if (IS_ERR(kbuf))
  449. return PTR_ERR(kbuf);
  450. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  451. return error;
  452. }
  453. int
  454. xfs_attrmulti_attr_remove(
  455. struct inode *inode,
  456. unsigned char *name,
  457. __uint32_t flags)
  458. {
  459. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  460. return EPERM;
  461. return xfs_attr_remove(XFS_I(inode), name, flags);
  462. }
  463. STATIC int
  464. xfs_attrmulti_by_handle(
  465. struct file *parfilp,
  466. void __user *arg)
  467. {
  468. int error;
  469. xfs_attr_multiop_t *ops;
  470. xfs_fsop_attrmulti_handlereq_t am_hreq;
  471. struct dentry *dentry;
  472. unsigned int i, size;
  473. unsigned char *attr_name;
  474. if (!capable(CAP_SYS_ADMIN))
  475. return -XFS_ERROR(EPERM);
  476. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  477. return -XFS_ERROR(EFAULT);
  478. /* overflow check */
  479. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  480. return -E2BIG;
  481. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  482. if (IS_ERR(dentry))
  483. return PTR_ERR(dentry);
  484. error = E2BIG;
  485. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  486. if (!size || size > 16 * PAGE_SIZE)
  487. goto out_dput;
  488. ops = memdup_user(am_hreq.ops, size);
  489. if (IS_ERR(ops)) {
  490. error = PTR_ERR(ops);
  491. goto out_dput;
  492. }
  493. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  494. if (!attr_name)
  495. goto out_kfree_ops;
  496. error = 0;
  497. for (i = 0; i < am_hreq.opcount; i++) {
  498. ops[i].am_error = strncpy_from_user((char *)attr_name,
  499. ops[i].am_attrname, MAXNAMELEN);
  500. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  501. error = -ERANGE;
  502. if (ops[i].am_error < 0)
  503. break;
  504. switch (ops[i].am_opcode) {
  505. case ATTR_OP_GET:
  506. ops[i].am_error = xfs_attrmulti_attr_get(
  507. dentry->d_inode, attr_name,
  508. ops[i].am_attrvalue, &ops[i].am_length,
  509. ops[i].am_flags);
  510. break;
  511. case ATTR_OP_SET:
  512. ops[i].am_error = mnt_want_write_file(parfilp);
  513. if (ops[i].am_error)
  514. break;
  515. ops[i].am_error = xfs_attrmulti_attr_set(
  516. dentry->d_inode, attr_name,
  517. ops[i].am_attrvalue, ops[i].am_length,
  518. ops[i].am_flags);
  519. mnt_drop_write_file(parfilp);
  520. break;
  521. case ATTR_OP_REMOVE:
  522. ops[i].am_error = mnt_want_write_file(parfilp);
  523. if (ops[i].am_error)
  524. break;
  525. ops[i].am_error = xfs_attrmulti_attr_remove(
  526. dentry->d_inode, attr_name,
  527. ops[i].am_flags);
  528. mnt_drop_write_file(parfilp);
  529. break;
  530. default:
  531. ops[i].am_error = EINVAL;
  532. }
  533. }
  534. if (copy_to_user(am_hreq.ops, ops, size))
  535. error = XFS_ERROR(EFAULT);
  536. kfree(attr_name);
  537. out_kfree_ops:
  538. kfree(ops);
  539. out_dput:
  540. dput(dentry);
  541. return -error;
  542. }
  543. int
  544. xfs_ioc_space(
  545. struct xfs_inode *ip,
  546. struct inode *inode,
  547. struct file *filp,
  548. int ioflags,
  549. unsigned int cmd,
  550. xfs_flock64_t *bf)
  551. {
  552. struct xfs_mount *mp = ip->i_mount;
  553. struct xfs_trans *tp;
  554. struct iattr iattr;
  555. bool setprealloc = false;
  556. bool clrprealloc = false;
  557. int error;
  558. /*
  559. * Only allow the sys admin to reserve space unless
  560. * unwritten extents are enabled.
  561. */
  562. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  563. !capable(CAP_SYS_ADMIN))
  564. return -XFS_ERROR(EPERM);
  565. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  566. return -XFS_ERROR(EPERM);
  567. if (!(filp->f_mode & FMODE_WRITE))
  568. return -XFS_ERROR(EBADF);
  569. if (!S_ISREG(inode->i_mode))
  570. return -XFS_ERROR(EINVAL);
  571. error = mnt_want_write_file(filp);
  572. if (error)
  573. return error;
  574. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  575. switch (bf->l_whence) {
  576. case 0: /*SEEK_SET*/
  577. break;
  578. case 1: /*SEEK_CUR*/
  579. bf->l_start += filp->f_pos;
  580. break;
  581. case 2: /*SEEK_END*/
  582. bf->l_start += XFS_ISIZE(ip);
  583. break;
  584. default:
  585. error = XFS_ERROR(EINVAL);
  586. goto out_unlock;
  587. }
  588. /*
  589. * length of <= 0 for resv/unresv/zero is invalid. length for
  590. * alloc/free is ignored completely and we have no idea what userspace
  591. * might have set it to, so set it to zero to allow range
  592. * checks to pass.
  593. */
  594. switch (cmd) {
  595. case XFS_IOC_ZERO_RANGE:
  596. case XFS_IOC_RESVSP:
  597. case XFS_IOC_RESVSP64:
  598. case XFS_IOC_UNRESVSP:
  599. case XFS_IOC_UNRESVSP64:
  600. if (bf->l_len <= 0) {
  601. error = XFS_ERROR(EINVAL);
  602. goto out_unlock;
  603. }
  604. break;
  605. default:
  606. bf->l_len = 0;
  607. break;
  608. }
  609. if (bf->l_start < 0 ||
  610. bf->l_start > mp->m_super->s_maxbytes ||
  611. bf->l_start + bf->l_len < 0 ||
  612. bf->l_start + bf->l_len >= mp->m_super->s_maxbytes) {
  613. error = XFS_ERROR(EINVAL);
  614. goto out_unlock;
  615. }
  616. switch (cmd) {
  617. case XFS_IOC_ZERO_RANGE:
  618. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  619. if (!error)
  620. setprealloc = true;
  621. break;
  622. case XFS_IOC_RESVSP:
  623. case XFS_IOC_RESVSP64:
  624. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  625. XFS_BMAPI_PREALLOC);
  626. if (!error)
  627. setprealloc = true;
  628. break;
  629. case XFS_IOC_UNRESVSP:
  630. case XFS_IOC_UNRESVSP64:
  631. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  632. break;
  633. case XFS_IOC_ALLOCSP:
  634. case XFS_IOC_ALLOCSP64:
  635. case XFS_IOC_FREESP:
  636. case XFS_IOC_FREESP64:
  637. if (bf->l_start > XFS_ISIZE(ip)) {
  638. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  639. bf->l_start - XFS_ISIZE(ip), 0);
  640. if (error)
  641. goto out_unlock;
  642. }
  643. iattr.ia_valid = ATTR_SIZE;
  644. iattr.ia_size = bf->l_start;
  645. error = xfs_setattr_size(ip, &iattr);
  646. if (!error)
  647. clrprealloc = true;
  648. break;
  649. default:
  650. ASSERT(0);
  651. error = XFS_ERROR(EINVAL);
  652. }
  653. if (error)
  654. goto out_unlock;
  655. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  656. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_writeid, 0, 0);
  657. if (error) {
  658. xfs_trans_cancel(tp, 0);
  659. goto out_unlock;
  660. }
  661. xfs_ilock(ip, XFS_ILOCK_EXCL);
  662. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  663. if (!(ioflags & IO_INVIS)) {
  664. ip->i_d.di_mode &= ~S_ISUID;
  665. if (ip->i_d.di_mode & S_IXGRP)
  666. ip->i_d.di_mode &= ~S_ISGID;
  667. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  668. }
  669. if (setprealloc)
  670. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  671. else if (clrprealloc)
  672. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  673. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  674. if (filp->f_flags & O_DSYNC)
  675. xfs_trans_set_sync(tp);
  676. error = xfs_trans_commit(tp, 0);
  677. out_unlock:
  678. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  679. mnt_drop_write_file(filp);
  680. return -error;
  681. }
  682. STATIC int
  683. xfs_ioc_bulkstat(
  684. xfs_mount_t *mp,
  685. unsigned int cmd,
  686. void __user *arg)
  687. {
  688. xfs_fsop_bulkreq_t bulkreq;
  689. int count; /* # of records returned */
  690. xfs_ino_t inlast; /* last inode number */
  691. int done;
  692. int error;
  693. /* done = 1 if there are more stats to get and if bulkstat */
  694. /* should be called again (unused here, but used in dmapi) */
  695. if (!capable(CAP_SYS_ADMIN))
  696. return -EPERM;
  697. if (XFS_FORCED_SHUTDOWN(mp))
  698. return -XFS_ERROR(EIO);
  699. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  700. return -XFS_ERROR(EFAULT);
  701. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  702. return -XFS_ERROR(EFAULT);
  703. if ((count = bulkreq.icount) <= 0)
  704. return -XFS_ERROR(EINVAL);
  705. if (bulkreq.ubuffer == NULL)
  706. return -XFS_ERROR(EINVAL);
  707. if (cmd == XFS_IOC_FSINUMBERS)
  708. error = xfs_inumbers(mp, &inlast, &count,
  709. bulkreq.ubuffer, xfs_inumbers_fmt);
  710. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  711. error = xfs_bulkstat_single(mp, &inlast,
  712. bulkreq.ubuffer, &done);
  713. else /* XFS_IOC_FSBULKSTAT */
  714. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  715. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  716. &done);
  717. if (error)
  718. return -error;
  719. if (bulkreq.ocount != NULL) {
  720. if (copy_to_user(bulkreq.lastip, &inlast,
  721. sizeof(xfs_ino_t)))
  722. return -XFS_ERROR(EFAULT);
  723. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  724. return -XFS_ERROR(EFAULT);
  725. }
  726. return 0;
  727. }
  728. STATIC int
  729. xfs_ioc_fsgeometry_v1(
  730. xfs_mount_t *mp,
  731. void __user *arg)
  732. {
  733. xfs_fsop_geom_t fsgeo;
  734. int error;
  735. error = xfs_fs_geometry(mp, &fsgeo, 3);
  736. if (error)
  737. return -error;
  738. /*
  739. * Caller should have passed an argument of type
  740. * xfs_fsop_geom_v1_t. This is a proper subset of the
  741. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  742. */
  743. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  744. return -XFS_ERROR(EFAULT);
  745. return 0;
  746. }
  747. STATIC int
  748. xfs_ioc_fsgeometry(
  749. xfs_mount_t *mp,
  750. void __user *arg)
  751. {
  752. xfs_fsop_geom_t fsgeo;
  753. int error;
  754. error = xfs_fs_geometry(mp, &fsgeo, 4);
  755. if (error)
  756. return -error;
  757. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  758. return -XFS_ERROR(EFAULT);
  759. return 0;
  760. }
  761. /*
  762. * Linux extended inode flags interface.
  763. */
  764. STATIC unsigned int
  765. xfs_merge_ioc_xflags(
  766. unsigned int flags,
  767. unsigned int start)
  768. {
  769. unsigned int xflags = start;
  770. if (flags & FS_IMMUTABLE_FL)
  771. xflags |= XFS_XFLAG_IMMUTABLE;
  772. else
  773. xflags &= ~XFS_XFLAG_IMMUTABLE;
  774. if (flags & FS_APPEND_FL)
  775. xflags |= XFS_XFLAG_APPEND;
  776. else
  777. xflags &= ~XFS_XFLAG_APPEND;
  778. if (flags & FS_SYNC_FL)
  779. xflags |= XFS_XFLAG_SYNC;
  780. else
  781. xflags &= ~XFS_XFLAG_SYNC;
  782. if (flags & FS_NOATIME_FL)
  783. xflags |= XFS_XFLAG_NOATIME;
  784. else
  785. xflags &= ~XFS_XFLAG_NOATIME;
  786. if (flags & FS_NODUMP_FL)
  787. xflags |= XFS_XFLAG_NODUMP;
  788. else
  789. xflags &= ~XFS_XFLAG_NODUMP;
  790. return xflags;
  791. }
  792. STATIC unsigned int
  793. xfs_di2lxflags(
  794. __uint16_t di_flags)
  795. {
  796. unsigned int flags = 0;
  797. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  798. flags |= FS_IMMUTABLE_FL;
  799. if (di_flags & XFS_DIFLAG_APPEND)
  800. flags |= FS_APPEND_FL;
  801. if (di_flags & XFS_DIFLAG_SYNC)
  802. flags |= FS_SYNC_FL;
  803. if (di_flags & XFS_DIFLAG_NOATIME)
  804. flags |= FS_NOATIME_FL;
  805. if (di_flags & XFS_DIFLAG_NODUMP)
  806. flags |= FS_NODUMP_FL;
  807. return flags;
  808. }
  809. STATIC int
  810. xfs_ioc_fsgetxattr(
  811. xfs_inode_t *ip,
  812. int attr,
  813. void __user *arg)
  814. {
  815. struct fsxattr fa;
  816. memset(&fa, 0, sizeof(struct fsxattr));
  817. xfs_ilock(ip, XFS_ILOCK_SHARED);
  818. fa.fsx_xflags = xfs_ip2xflags(ip);
  819. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  820. fa.fsx_projid = xfs_get_projid(ip);
  821. if (attr) {
  822. if (ip->i_afp) {
  823. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  824. fa.fsx_nextents = ip->i_afp->if_bytes /
  825. sizeof(xfs_bmbt_rec_t);
  826. else
  827. fa.fsx_nextents = ip->i_d.di_anextents;
  828. } else
  829. fa.fsx_nextents = 0;
  830. } else {
  831. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  832. fa.fsx_nextents = ip->i_df.if_bytes /
  833. sizeof(xfs_bmbt_rec_t);
  834. else
  835. fa.fsx_nextents = ip->i_d.di_nextents;
  836. }
  837. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  838. if (copy_to_user(arg, &fa, sizeof(fa)))
  839. return -EFAULT;
  840. return 0;
  841. }
  842. STATIC void
  843. xfs_set_diflags(
  844. struct xfs_inode *ip,
  845. unsigned int xflags)
  846. {
  847. unsigned int di_flags;
  848. /* can't set PREALLOC this way, just preserve it */
  849. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  850. if (xflags & XFS_XFLAG_IMMUTABLE)
  851. di_flags |= XFS_DIFLAG_IMMUTABLE;
  852. if (xflags & XFS_XFLAG_APPEND)
  853. di_flags |= XFS_DIFLAG_APPEND;
  854. if (xflags & XFS_XFLAG_SYNC)
  855. di_flags |= XFS_DIFLAG_SYNC;
  856. if (xflags & XFS_XFLAG_NOATIME)
  857. di_flags |= XFS_DIFLAG_NOATIME;
  858. if (xflags & XFS_XFLAG_NODUMP)
  859. di_flags |= XFS_DIFLAG_NODUMP;
  860. if (xflags & XFS_XFLAG_PROJINHERIT)
  861. di_flags |= XFS_DIFLAG_PROJINHERIT;
  862. if (xflags & XFS_XFLAG_NODEFRAG)
  863. di_flags |= XFS_DIFLAG_NODEFRAG;
  864. if (xflags & XFS_XFLAG_FILESTREAM)
  865. di_flags |= XFS_DIFLAG_FILESTREAM;
  866. if (S_ISDIR(ip->i_d.di_mode)) {
  867. if (xflags & XFS_XFLAG_RTINHERIT)
  868. di_flags |= XFS_DIFLAG_RTINHERIT;
  869. if (xflags & XFS_XFLAG_NOSYMLINKS)
  870. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  871. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  872. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  873. } else if (S_ISREG(ip->i_d.di_mode)) {
  874. if (xflags & XFS_XFLAG_REALTIME)
  875. di_flags |= XFS_DIFLAG_REALTIME;
  876. if (xflags & XFS_XFLAG_EXTSIZE)
  877. di_flags |= XFS_DIFLAG_EXTSIZE;
  878. }
  879. ip->i_d.di_flags = di_flags;
  880. }
  881. STATIC void
  882. xfs_diflags_to_linux(
  883. struct xfs_inode *ip)
  884. {
  885. struct inode *inode = VFS_I(ip);
  886. unsigned int xflags = xfs_ip2xflags(ip);
  887. if (xflags & XFS_XFLAG_IMMUTABLE)
  888. inode->i_flags |= S_IMMUTABLE;
  889. else
  890. inode->i_flags &= ~S_IMMUTABLE;
  891. if (xflags & XFS_XFLAG_APPEND)
  892. inode->i_flags |= S_APPEND;
  893. else
  894. inode->i_flags &= ~S_APPEND;
  895. if (xflags & XFS_XFLAG_SYNC)
  896. inode->i_flags |= S_SYNC;
  897. else
  898. inode->i_flags &= ~S_SYNC;
  899. if (xflags & XFS_XFLAG_NOATIME)
  900. inode->i_flags |= S_NOATIME;
  901. else
  902. inode->i_flags &= ~S_NOATIME;
  903. }
  904. #define FSX_PROJID 1
  905. #define FSX_EXTSIZE 2
  906. #define FSX_XFLAGS 4
  907. #define FSX_NONBLOCK 8
  908. STATIC int
  909. xfs_ioctl_setattr(
  910. xfs_inode_t *ip,
  911. struct fsxattr *fa,
  912. int mask)
  913. {
  914. struct xfs_mount *mp = ip->i_mount;
  915. struct xfs_trans *tp;
  916. unsigned int lock_flags = 0;
  917. struct xfs_dquot *udqp = NULL;
  918. struct xfs_dquot *pdqp = NULL;
  919. struct xfs_dquot *olddquot = NULL;
  920. int code;
  921. trace_xfs_ioctl_setattr(ip);
  922. if (mp->m_flags & XFS_MOUNT_RDONLY)
  923. return XFS_ERROR(EROFS);
  924. if (XFS_FORCED_SHUTDOWN(mp))
  925. return XFS_ERROR(EIO);
  926. /*
  927. * Disallow 32bit project ids when projid32bit feature is not enabled.
  928. */
  929. if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
  930. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  931. return XFS_ERROR(EINVAL);
  932. /*
  933. * If disk quotas is on, we make sure that the dquots do exist on disk,
  934. * before we start any other transactions. Trying to do this later
  935. * is messy. We don't care to take a readlock to look at the ids
  936. * in inode here, because we can't hold it across the trans_reserve.
  937. * If the IDs do change before we take the ilock, we're covered
  938. * because the i_*dquot fields will get updated anyway.
  939. */
  940. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  941. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  942. ip->i_d.di_gid, fa->fsx_projid,
  943. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  944. if (code)
  945. return code;
  946. }
  947. /*
  948. * For the other attributes, we acquire the inode lock and
  949. * first do an error checking pass.
  950. */
  951. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  952. code = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  953. if (code)
  954. goto error_return;
  955. lock_flags = XFS_ILOCK_EXCL;
  956. xfs_ilock(ip, lock_flags);
  957. /*
  958. * CAP_FOWNER overrides the following restrictions:
  959. *
  960. * The user ID of the calling process must be equal
  961. * to the file owner ID, except in cases where the
  962. * CAP_FSETID capability is applicable.
  963. */
  964. if (!inode_owner_or_capable(VFS_I(ip))) {
  965. code = XFS_ERROR(EPERM);
  966. goto error_return;
  967. }
  968. /*
  969. * Do a quota reservation only if projid is actually going to change.
  970. * Only allow changing of projid from init_user_ns since it is a
  971. * non user namespace aware identifier.
  972. */
  973. if (mask & FSX_PROJID) {
  974. if (current_user_ns() != &init_user_ns) {
  975. code = XFS_ERROR(EINVAL);
  976. goto error_return;
  977. }
  978. if (XFS_IS_QUOTA_RUNNING(mp) &&
  979. XFS_IS_PQUOTA_ON(mp) &&
  980. xfs_get_projid(ip) != fa->fsx_projid) {
  981. ASSERT(tp);
  982. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
  983. pdqp, capable(CAP_FOWNER) ?
  984. XFS_QMOPT_FORCE_RES : 0);
  985. if (code) /* out of quota */
  986. goto error_return;
  987. }
  988. }
  989. if (mask & FSX_EXTSIZE) {
  990. /*
  991. * Can't change extent size if any extents are allocated.
  992. */
  993. if (ip->i_d.di_nextents &&
  994. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  995. fa->fsx_extsize)) {
  996. code = XFS_ERROR(EINVAL); /* EFBIG? */
  997. goto error_return;
  998. }
  999. /*
  1000. * Extent size must be a multiple of the appropriate block
  1001. * size, if set at all. It must also be smaller than the
  1002. * maximum extent size supported by the filesystem.
  1003. *
  1004. * Also, for non-realtime files, limit the extent size hint to
  1005. * half the size of the AGs in the filesystem so alignment
  1006. * doesn't result in extents larger than an AG.
  1007. */
  1008. if (fa->fsx_extsize != 0) {
  1009. xfs_extlen_t size;
  1010. xfs_fsblock_t extsize_fsb;
  1011. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1012. if (extsize_fsb > MAXEXTLEN) {
  1013. code = XFS_ERROR(EINVAL);
  1014. goto error_return;
  1015. }
  1016. if (XFS_IS_REALTIME_INODE(ip) ||
  1017. ((mask & FSX_XFLAGS) &&
  1018. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  1019. size = mp->m_sb.sb_rextsize <<
  1020. mp->m_sb.sb_blocklog;
  1021. } else {
  1022. size = mp->m_sb.sb_blocksize;
  1023. if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
  1024. code = XFS_ERROR(EINVAL);
  1025. goto error_return;
  1026. }
  1027. }
  1028. if (fa->fsx_extsize % size) {
  1029. code = XFS_ERROR(EINVAL);
  1030. goto error_return;
  1031. }
  1032. }
  1033. }
  1034. if (mask & FSX_XFLAGS) {
  1035. /*
  1036. * Can't change realtime flag if any extents are allocated.
  1037. */
  1038. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  1039. (XFS_IS_REALTIME_INODE(ip)) !=
  1040. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  1041. code = XFS_ERROR(EINVAL); /* EFBIG? */
  1042. goto error_return;
  1043. }
  1044. /*
  1045. * If realtime flag is set then must have realtime data.
  1046. */
  1047. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  1048. if ((mp->m_sb.sb_rblocks == 0) ||
  1049. (mp->m_sb.sb_rextsize == 0) ||
  1050. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  1051. code = XFS_ERROR(EINVAL);
  1052. goto error_return;
  1053. }
  1054. }
  1055. /*
  1056. * Can't modify an immutable/append-only file unless
  1057. * we have appropriate permission.
  1058. */
  1059. if ((ip->i_d.di_flags &
  1060. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  1061. (fa->fsx_xflags &
  1062. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  1063. !capable(CAP_LINUX_IMMUTABLE)) {
  1064. code = XFS_ERROR(EPERM);
  1065. goto error_return;
  1066. }
  1067. }
  1068. xfs_trans_ijoin(tp, ip, 0);
  1069. /*
  1070. * Change file ownership. Must be the owner or privileged.
  1071. */
  1072. if (mask & FSX_PROJID) {
  1073. /*
  1074. * CAP_FSETID overrides the following restrictions:
  1075. *
  1076. * The set-user-ID and set-group-ID bits of a file will be
  1077. * cleared upon successful return from chown()
  1078. */
  1079. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  1080. !inode_capable(VFS_I(ip), CAP_FSETID))
  1081. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  1082. /*
  1083. * Change the ownerships and register quota modifications
  1084. * in the transaction.
  1085. */
  1086. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1087. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1088. olddquot = xfs_qm_vop_chown(tp, ip,
  1089. &ip->i_pdquot, pdqp);
  1090. }
  1091. xfs_set_projid(ip, fa->fsx_projid);
  1092. /*
  1093. * We may have to rev the inode as well as
  1094. * the superblock version number since projids didn't
  1095. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  1096. */
  1097. if (ip->i_d.di_version == 1)
  1098. xfs_bump_ino_vers2(tp, ip);
  1099. }
  1100. }
  1101. if (mask & FSX_EXTSIZE)
  1102. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1103. if (mask & FSX_XFLAGS) {
  1104. xfs_set_diflags(ip, fa->fsx_xflags);
  1105. xfs_diflags_to_linux(ip);
  1106. }
  1107. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  1108. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1109. XFS_STATS_INC(xs_ig_attrchg);
  1110. /*
  1111. * If this is a synchronous mount, make sure that the
  1112. * transaction goes to disk before returning to the user.
  1113. * This is slightly sub-optimal in that truncates require
  1114. * two sync transactions instead of one for wsync filesystems.
  1115. * One for the truncate and one for the timestamps since we
  1116. * don't want to change the timestamps unless we're sure the
  1117. * truncate worked. Truncates are less than 1% of the laddis
  1118. * mix so this probably isn't worth the trouble to optimize.
  1119. */
  1120. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1121. xfs_trans_set_sync(tp);
  1122. code = xfs_trans_commit(tp, 0);
  1123. xfs_iunlock(ip, lock_flags);
  1124. /*
  1125. * Release any dquot(s) the inode had kept before chown.
  1126. */
  1127. xfs_qm_dqrele(olddquot);
  1128. xfs_qm_dqrele(udqp);
  1129. xfs_qm_dqrele(pdqp);
  1130. return code;
  1131. error_return:
  1132. xfs_qm_dqrele(udqp);
  1133. xfs_qm_dqrele(pdqp);
  1134. xfs_trans_cancel(tp, 0);
  1135. if (lock_flags)
  1136. xfs_iunlock(ip, lock_flags);
  1137. return code;
  1138. }
  1139. STATIC int
  1140. xfs_ioc_fssetxattr(
  1141. xfs_inode_t *ip,
  1142. struct file *filp,
  1143. void __user *arg)
  1144. {
  1145. struct fsxattr fa;
  1146. unsigned int mask;
  1147. int error;
  1148. if (copy_from_user(&fa, arg, sizeof(fa)))
  1149. return -EFAULT;
  1150. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  1151. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1152. mask |= FSX_NONBLOCK;
  1153. error = mnt_want_write_file(filp);
  1154. if (error)
  1155. return error;
  1156. error = xfs_ioctl_setattr(ip, &fa, mask);
  1157. mnt_drop_write_file(filp);
  1158. return -error;
  1159. }
  1160. STATIC int
  1161. xfs_ioc_getxflags(
  1162. xfs_inode_t *ip,
  1163. void __user *arg)
  1164. {
  1165. unsigned int flags;
  1166. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1167. if (copy_to_user(arg, &flags, sizeof(flags)))
  1168. return -EFAULT;
  1169. return 0;
  1170. }
  1171. STATIC int
  1172. xfs_ioc_setxflags(
  1173. xfs_inode_t *ip,
  1174. struct file *filp,
  1175. void __user *arg)
  1176. {
  1177. struct fsxattr fa;
  1178. unsigned int flags;
  1179. unsigned int mask;
  1180. int error;
  1181. if (copy_from_user(&flags, arg, sizeof(flags)))
  1182. return -EFAULT;
  1183. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1184. FS_NOATIME_FL | FS_NODUMP_FL | \
  1185. FS_SYNC_FL))
  1186. return -EOPNOTSUPP;
  1187. mask = FSX_XFLAGS;
  1188. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1189. mask |= FSX_NONBLOCK;
  1190. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1191. error = mnt_want_write_file(filp);
  1192. if (error)
  1193. return error;
  1194. error = xfs_ioctl_setattr(ip, &fa, mask);
  1195. mnt_drop_write_file(filp);
  1196. return -error;
  1197. }
  1198. STATIC int
  1199. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1200. {
  1201. struct getbmap __user *base = *ap;
  1202. /* copy only getbmap portion (not getbmapx) */
  1203. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1204. return XFS_ERROR(EFAULT);
  1205. *ap += sizeof(struct getbmap);
  1206. return 0;
  1207. }
  1208. STATIC int
  1209. xfs_ioc_getbmap(
  1210. struct xfs_inode *ip,
  1211. int ioflags,
  1212. unsigned int cmd,
  1213. void __user *arg)
  1214. {
  1215. struct getbmapx bmx;
  1216. int error;
  1217. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1218. return -XFS_ERROR(EFAULT);
  1219. if (bmx.bmv_count < 2)
  1220. return -XFS_ERROR(EINVAL);
  1221. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1222. if (ioflags & IO_INVIS)
  1223. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1224. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1225. (struct getbmap *)arg+1);
  1226. if (error)
  1227. return -error;
  1228. /* copy back header - only size of getbmap */
  1229. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1230. return -XFS_ERROR(EFAULT);
  1231. return 0;
  1232. }
  1233. STATIC int
  1234. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1235. {
  1236. struct getbmapx __user *base = *ap;
  1237. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1238. return XFS_ERROR(EFAULT);
  1239. *ap += sizeof(struct getbmapx);
  1240. return 0;
  1241. }
  1242. STATIC int
  1243. xfs_ioc_getbmapx(
  1244. struct xfs_inode *ip,
  1245. void __user *arg)
  1246. {
  1247. struct getbmapx bmx;
  1248. int error;
  1249. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1250. return -XFS_ERROR(EFAULT);
  1251. if (bmx.bmv_count < 2)
  1252. return -XFS_ERROR(EINVAL);
  1253. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1254. return -XFS_ERROR(EINVAL);
  1255. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1256. (struct getbmapx *)arg+1);
  1257. if (error)
  1258. return -error;
  1259. /* copy back header */
  1260. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1261. return -XFS_ERROR(EFAULT);
  1262. return 0;
  1263. }
  1264. int
  1265. xfs_ioc_swapext(
  1266. xfs_swapext_t *sxp)
  1267. {
  1268. xfs_inode_t *ip, *tip;
  1269. struct fd f, tmp;
  1270. int error = 0;
  1271. /* Pull information for the target fd */
  1272. f = fdget((int)sxp->sx_fdtarget);
  1273. if (!f.file) {
  1274. error = XFS_ERROR(EINVAL);
  1275. goto out;
  1276. }
  1277. if (!(f.file->f_mode & FMODE_WRITE) ||
  1278. !(f.file->f_mode & FMODE_READ) ||
  1279. (f.file->f_flags & O_APPEND)) {
  1280. error = XFS_ERROR(EBADF);
  1281. goto out_put_file;
  1282. }
  1283. tmp = fdget((int)sxp->sx_fdtmp);
  1284. if (!tmp.file) {
  1285. error = XFS_ERROR(EINVAL);
  1286. goto out_put_file;
  1287. }
  1288. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1289. !(tmp.file->f_mode & FMODE_READ) ||
  1290. (tmp.file->f_flags & O_APPEND)) {
  1291. error = XFS_ERROR(EBADF);
  1292. goto out_put_tmp_file;
  1293. }
  1294. if (IS_SWAPFILE(file_inode(f.file)) ||
  1295. IS_SWAPFILE(file_inode(tmp.file))) {
  1296. error = XFS_ERROR(EINVAL);
  1297. goto out_put_tmp_file;
  1298. }
  1299. ip = XFS_I(file_inode(f.file));
  1300. tip = XFS_I(file_inode(tmp.file));
  1301. if (ip->i_mount != tip->i_mount) {
  1302. error = XFS_ERROR(EINVAL);
  1303. goto out_put_tmp_file;
  1304. }
  1305. if (ip->i_ino == tip->i_ino) {
  1306. error = XFS_ERROR(EINVAL);
  1307. goto out_put_tmp_file;
  1308. }
  1309. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1310. error = XFS_ERROR(EIO);
  1311. goto out_put_tmp_file;
  1312. }
  1313. error = xfs_swap_extents(ip, tip, sxp);
  1314. out_put_tmp_file:
  1315. fdput(tmp);
  1316. out_put_file:
  1317. fdput(f);
  1318. out:
  1319. return error;
  1320. }
  1321. /*
  1322. * Note: some of the ioctl's return positive numbers as a
  1323. * byte count indicating success, such as readlink_by_handle.
  1324. * So we don't "sign flip" like most other routines. This means
  1325. * true errors need to be returned as a negative value.
  1326. */
  1327. long
  1328. xfs_file_ioctl(
  1329. struct file *filp,
  1330. unsigned int cmd,
  1331. unsigned long p)
  1332. {
  1333. struct inode *inode = file_inode(filp);
  1334. struct xfs_inode *ip = XFS_I(inode);
  1335. struct xfs_mount *mp = ip->i_mount;
  1336. void __user *arg = (void __user *)p;
  1337. int ioflags = 0;
  1338. int error;
  1339. if (filp->f_mode & FMODE_NOCMTIME)
  1340. ioflags |= IO_INVIS;
  1341. trace_xfs_file_ioctl(ip);
  1342. switch (cmd) {
  1343. case FITRIM:
  1344. return xfs_ioc_trim(mp, arg);
  1345. case XFS_IOC_ALLOCSP:
  1346. case XFS_IOC_FREESP:
  1347. case XFS_IOC_RESVSP:
  1348. case XFS_IOC_UNRESVSP:
  1349. case XFS_IOC_ALLOCSP64:
  1350. case XFS_IOC_FREESP64:
  1351. case XFS_IOC_RESVSP64:
  1352. case XFS_IOC_UNRESVSP64:
  1353. case XFS_IOC_ZERO_RANGE: {
  1354. xfs_flock64_t bf;
  1355. if (copy_from_user(&bf, arg, sizeof(bf)))
  1356. return -XFS_ERROR(EFAULT);
  1357. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1358. }
  1359. case XFS_IOC_DIOINFO: {
  1360. struct dioattr da;
  1361. xfs_buftarg_t *target =
  1362. XFS_IS_REALTIME_INODE(ip) ?
  1363. mp->m_rtdev_targp : mp->m_ddev_targp;
  1364. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1365. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1366. if (copy_to_user(arg, &da, sizeof(da)))
  1367. return -XFS_ERROR(EFAULT);
  1368. return 0;
  1369. }
  1370. case XFS_IOC_FSBULKSTAT_SINGLE:
  1371. case XFS_IOC_FSBULKSTAT:
  1372. case XFS_IOC_FSINUMBERS:
  1373. return xfs_ioc_bulkstat(mp, cmd, arg);
  1374. case XFS_IOC_FSGEOMETRY_V1:
  1375. return xfs_ioc_fsgeometry_v1(mp, arg);
  1376. case XFS_IOC_FSGEOMETRY:
  1377. return xfs_ioc_fsgeometry(mp, arg);
  1378. case XFS_IOC_GETVERSION:
  1379. return put_user(inode->i_generation, (int __user *)arg);
  1380. case XFS_IOC_FSGETXATTR:
  1381. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1382. case XFS_IOC_FSGETXATTRA:
  1383. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1384. case XFS_IOC_FSSETXATTR:
  1385. return xfs_ioc_fssetxattr(ip, filp, arg);
  1386. case XFS_IOC_GETXFLAGS:
  1387. return xfs_ioc_getxflags(ip, arg);
  1388. case XFS_IOC_SETXFLAGS:
  1389. return xfs_ioc_setxflags(ip, filp, arg);
  1390. case XFS_IOC_FSSETDM: {
  1391. struct fsdmidata dmi;
  1392. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1393. return -XFS_ERROR(EFAULT);
  1394. error = mnt_want_write_file(filp);
  1395. if (error)
  1396. return error;
  1397. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1398. dmi.fsd_dmstate);
  1399. mnt_drop_write_file(filp);
  1400. return -error;
  1401. }
  1402. case XFS_IOC_GETBMAP:
  1403. case XFS_IOC_GETBMAPA:
  1404. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1405. case XFS_IOC_GETBMAPX:
  1406. return xfs_ioc_getbmapx(ip, arg);
  1407. case XFS_IOC_FD_TO_HANDLE:
  1408. case XFS_IOC_PATH_TO_HANDLE:
  1409. case XFS_IOC_PATH_TO_FSHANDLE: {
  1410. xfs_fsop_handlereq_t hreq;
  1411. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1412. return -XFS_ERROR(EFAULT);
  1413. return xfs_find_handle(cmd, &hreq);
  1414. }
  1415. case XFS_IOC_OPEN_BY_HANDLE: {
  1416. xfs_fsop_handlereq_t hreq;
  1417. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1418. return -XFS_ERROR(EFAULT);
  1419. return xfs_open_by_handle(filp, &hreq);
  1420. }
  1421. case XFS_IOC_FSSETDM_BY_HANDLE:
  1422. return xfs_fssetdm_by_handle(filp, arg);
  1423. case XFS_IOC_READLINK_BY_HANDLE: {
  1424. xfs_fsop_handlereq_t hreq;
  1425. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1426. return -XFS_ERROR(EFAULT);
  1427. return xfs_readlink_by_handle(filp, &hreq);
  1428. }
  1429. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1430. return xfs_attrlist_by_handle(filp, arg);
  1431. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1432. return xfs_attrmulti_by_handle(filp, arg);
  1433. case XFS_IOC_SWAPEXT: {
  1434. struct xfs_swapext sxp;
  1435. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1436. return -XFS_ERROR(EFAULT);
  1437. error = mnt_want_write_file(filp);
  1438. if (error)
  1439. return error;
  1440. error = xfs_ioc_swapext(&sxp);
  1441. mnt_drop_write_file(filp);
  1442. return -error;
  1443. }
  1444. case XFS_IOC_FSCOUNTS: {
  1445. xfs_fsop_counts_t out;
  1446. error = xfs_fs_counts(mp, &out);
  1447. if (error)
  1448. return -error;
  1449. if (copy_to_user(arg, &out, sizeof(out)))
  1450. return -XFS_ERROR(EFAULT);
  1451. return 0;
  1452. }
  1453. case XFS_IOC_SET_RESBLKS: {
  1454. xfs_fsop_resblks_t inout;
  1455. __uint64_t in;
  1456. if (!capable(CAP_SYS_ADMIN))
  1457. return -EPERM;
  1458. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1459. return -XFS_ERROR(EROFS);
  1460. if (copy_from_user(&inout, arg, sizeof(inout)))
  1461. return -XFS_ERROR(EFAULT);
  1462. error = mnt_want_write_file(filp);
  1463. if (error)
  1464. return error;
  1465. /* input parameter is passed in resblks field of structure */
  1466. in = inout.resblks;
  1467. error = xfs_reserve_blocks(mp, &in, &inout);
  1468. mnt_drop_write_file(filp);
  1469. if (error)
  1470. return -error;
  1471. if (copy_to_user(arg, &inout, sizeof(inout)))
  1472. return -XFS_ERROR(EFAULT);
  1473. return 0;
  1474. }
  1475. case XFS_IOC_GET_RESBLKS: {
  1476. xfs_fsop_resblks_t out;
  1477. if (!capable(CAP_SYS_ADMIN))
  1478. return -EPERM;
  1479. error = xfs_reserve_blocks(mp, NULL, &out);
  1480. if (error)
  1481. return -error;
  1482. if (copy_to_user(arg, &out, sizeof(out)))
  1483. return -XFS_ERROR(EFAULT);
  1484. return 0;
  1485. }
  1486. case XFS_IOC_FSGROWFSDATA: {
  1487. xfs_growfs_data_t in;
  1488. if (copy_from_user(&in, arg, sizeof(in)))
  1489. return -XFS_ERROR(EFAULT);
  1490. error = mnt_want_write_file(filp);
  1491. if (error)
  1492. return error;
  1493. error = xfs_growfs_data(mp, &in);
  1494. mnt_drop_write_file(filp);
  1495. return -error;
  1496. }
  1497. case XFS_IOC_FSGROWFSLOG: {
  1498. xfs_growfs_log_t in;
  1499. if (copy_from_user(&in, arg, sizeof(in)))
  1500. return -XFS_ERROR(EFAULT);
  1501. error = mnt_want_write_file(filp);
  1502. if (error)
  1503. return error;
  1504. error = xfs_growfs_log(mp, &in);
  1505. mnt_drop_write_file(filp);
  1506. return -error;
  1507. }
  1508. case XFS_IOC_FSGROWFSRT: {
  1509. xfs_growfs_rt_t in;
  1510. if (copy_from_user(&in, arg, sizeof(in)))
  1511. return -XFS_ERROR(EFAULT);
  1512. error = mnt_want_write_file(filp);
  1513. if (error)
  1514. return error;
  1515. error = xfs_growfs_rt(mp, &in);
  1516. mnt_drop_write_file(filp);
  1517. return -error;
  1518. }
  1519. case XFS_IOC_GOINGDOWN: {
  1520. __uint32_t in;
  1521. if (!capable(CAP_SYS_ADMIN))
  1522. return -EPERM;
  1523. if (get_user(in, (__uint32_t __user *)arg))
  1524. return -XFS_ERROR(EFAULT);
  1525. error = xfs_fs_goingdown(mp, in);
  1526. return -error;
  1527. }
  1528. case XFS_IOC_ERROR_INJECTION: {
  1529. xfs_error_injection_t in;
  1530. if (!capable(CAP_SYS_ADMIN))
  1531. return -EPERM;
  1532. if (copy_from_user(&in, arg, sizeof(in)))
  1533. return -XFS_ERROR(EFAULT);
  1534. error = xfs_errortag_add(in.errtag, mp);
  1535. return -error;
  1536. }
  1537. case XFS_IOC_ERROR_CLEARALL:
  1538. if (!capable(CAP_SYS_ADMIN))
  1539. return -EPERM;
  1540. error = xfs_errortag_clearall(mp, 1);
  1541. return -error;
  1542. case XFS_IOC_FREE_EOFBLOCKS: {
  1543. struct xfs_fs_eofblocks eofb;
  1544. struct xfs_eofblocks keofb;
  1545. if (!capable(CAP_SYS_ADMIN))
  1546. return -EPERM;
  1547. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1548. return -XFS_ERROR(EROFS);
  1549. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1550. return -XFS_ERROR(EFAULT);
  1551. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1552. if (error)
  1553. return -error;
  1554. return -xfs_icache_free_eofblocks(mp, &keofb);
  1555. }
  1556. default:
  1557. return -ENOTTY;
  1558. }
  1559. }