xfs_ioctl.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_rtalloc.h"
  29. #include "xfs_itable.h"
  30. #include "xfs_error.h"
  31. #include "xfs_attr.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_fsops.h"
  35. #include "xfs_discard.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_export.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_acl.h"
  44. #include <linux/capability.h>
  45. #include <linux/dcache.h>
  46. #include <linux/mount.h>
  47. #include <linux/namei.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/slab.h>
  50. #include <linux/exportfs.h>
  51. /*
  52. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  53. * a file or fs handle.
  54. *
  55. * XFS_IOC_PATH_TO_FSHANDLE
  56. * returns fs handle for a mount point or path within that mount point
  57. * XFS_IOC_FD_TO_HANDLE
  58. * returns full handle for a FD opened in user space
  59. * XFS_IOC_PATH_TO_HANDLE
  60. * returns full handle for a path
  61. */
  62. int
  63. xfs_find_handle(
  64. unsigned int cmd,
  65. xfs_fsop_handlereq_t *hreq)
  66. {
  67. int hsize;
  68. xfs_handle_t handle;
  69. struct inode *inode;
  70. struct fd f = {NULL};
  71. struct path path;
  72. int error;
  73. struct xfs_inode *ip;
  74. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  75. f = fdget(hreq->fd);
  76. if (!f.file)
  77. return -EBADF;
  78. inode = file_inode(f.file);
  79. } else {
  80. error = user_lpath((const char __user *)hreq->path, &path);
  81. if (error)
  82. return error;
  83. inode = d_inode(path.dentry);
  84. }
  85. ip = XFS_I(inode);
  86. /*
  87. * We can only generate handles for inodes residing on a XFS filesystem,
  88. * and only for regular files, directories or symbolic links.
  89. */
  90. error = -EINVAL;
  91. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  92. goto out_put;
  93. error = -EBADF;
  94. if (!S_ISREG(inode->i_mode) &&
  95. !S_ISDIR(inode->i_mode) &&
  96. !S_ISLNK(inode->i_mode))
  97. goto out_put;
  98. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  99. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  100. /*
  101. * This handle only contains an fsid, zero the rest.
  102. */
  103. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  104. hsize = sizeof(xfs_fsid_t);
  105. } else {
  106. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  107. sizeof(handle.ha_fid.fid_len);
  108. handle.ha_fid.fid_pad = 0;
  109. handle.ha_fid.fid_gen = inode->i_generation;
  110. handle.ha_fid.fid_ino = ip->i_ino;
  111. hsize = XFS_HSIZE(handle);
  112. }
  113. error = -EFAULT;
  114. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  115. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  116. goto out_put;
  117. error = 0;
  118. out_put:
  119. if (cmd == XFS_IOC_FD_TO_HANDLE)
  120. fdput(f);
  121. else
  122. path_put(&path);
  123. return error;
  124. }
  125. /*
  126. * No need to do permission checks on the various pathname components
  127. * as the handle operations are privileged.
  128. */
  129. STATIC int
  130. xfs_handle_acceptable(
  131. void *context,
  132. struct dentry *dentry)
  133. {
  134. return 1;
  135. }
  136. /*
  137. * Convert userspace handle data into a dentry.
  138. */
  139. struct dentry *
  140. xfs_handle_to_dentry(
  141. struct file *parfilp,
  142. void __user *uhandle,
  143. u32 hlen)
  144. {
  145. xfs_handle_t handle;
  146. struct xfs_fid64 fid;
  147. /*
  148. * Only allow handle opens under a directory.
  149. */
  150. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  151. return ERR_PTR(-ENOTDIR);
  152. if (hlen != sizeof(xfs_handle_t))
  153. return ERR_PTR(-EINVAL);
  154. if (copy_from_user(&handle, uhandle, hlen))
  155. return ERR_PTR(-EFAULT);
  156. if (handle.ha_fid.fid_len !=
  157. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  158. return ERR_PTR(-EINVAL);
  159. memset(&fid, 0, sizeof(struct fid));
  160. fid.ino = handle.ha_fid.fid_ino;
  161. fid.gen = handle.ha_fid.fid_gen;
  162. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  163. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  164. xfs_handle_acceptable, NULL);
  165. }
  166. STATIC struct dentry *
  167. xfs_handlereq_to_dentry(
  168. struct file *parfilp,
  169. xfs_fsop_handlereq_t *hreq)
  170. {
  171. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  172. }
  173. int
  174. xfs_open_by_handle(
  175. struct file *parfilp,
  176. xfs_fsop_handlereq_t *hreq)
  177. {
  178. const struct cred *cred = current_cred();
  179. int error;
  180. int fd;
  181. int permflag;
  182. struct file *filp;
  183. struct inode *inode;
  184. struct dentry *dentry;
  185. fmode_t fmode;
  186. struct path path;
  187. if (!capable(CAP_SYS_ADMIN))
  188. return -EPERM;
  189. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  190. if (IS_ERR(dentry))
  191. return PTR_ERR(dentry);
  192. inode = d_inode(dentry);
  193. /* Restrict xfs_open_by_handle to directories & regular files. */
  194. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  195. error = -EPERM;
  196. goto out_dput;
  197. }
  198. #if BITS_PER_LONG != 32
  199. hreq->oflags |= O_LARGEFILE;
  200. #endif
  201. permflag = hreq->oflags;
  202. fmode = OPEN_FMODE(permflag);
  203. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  204. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  205. error = -EPERM;
  206. goto out_dput;
  207. }
  208. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  209. error = -EACCES;
  210. goto out_dput;
  211. }
  212. /* Can't write directories. */
  213. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  214. error = -EISDIR;
  215. goto out_dput;
  216. }
  217. fd = get_unused_fd_flags(0);
  218. if (fd < 0) {
  219. error = fd;
  220. goto out_dput;
  221. }
  222. path.mnt = parfilp->f_path.mnt;
  223. path.dentry = dentry;
  224. filp = dentry_open(&path, hreq->oflags, cred);
  225. dput(dentry);
  226. if (IS_ERR(filp)) {
  227. put_unused_fd(fd);
  228. return PTR_ERR(filp);
  229. }
  230. if (S_ISREG(inode->i_mode)) {
  231. filp->f_flags |= O_NOATIME;
  232. filp->f_mode |= FMODE_NOCMTIME;
  233. }
  234. fd_install(fd, filp);
  235. return fd;
  236. out_dput:
  237. dput(dentry);
  238. return error;
  239. }
  240. int
  241. xfs_readlink_by_handle(
  242. struct file *parfilp,
  243. xfs_fsop_handlereq_t *hreq)
  244. {
  245. struct dentry *dentry;
  246. __u32 olen;
  247. int error;
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -EPERM;
  250. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  251. if (IS_ERR(dentry))
  252. return PTR_ERR(dentry);
  253. /* Restrict this handle operation to symlinks only. */
  254. if (!d_inode(dentry)->i_op->readlink) {
  255. error = -EINVAL;
  256. goto out_dput;
  257. }
  258. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  259. error = -EFAULT;
  260. goto out_dput;
  261. }
  262. error = d_inode(dentry)->i_op->readlink(dentry, hreq->ohandle, olen);
  263. out_dput:
  264. dput(dentry);
  265. return error;
  266. }
  267. int
  268. xfs_set_dmattrs(
  269. xfs_inode_t *ip,
  270. u_int evmask,
  271. u_int16_t state)
  272. {
  273. xfs_mount_t *mp = ip->i_mount;
  274. xfs_trans_t *tp;
  275. int error;
  276. if (!capable(CAP_SYS_ADMIN))
  277. return -EPERM;
  278. if (XFS_FORCED_SHUTDOWN(mp))
  279. return -EIO;
  280. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  281. if (error)
  282. return error;
  283. xfs_ilock(ip, XFS_ILOCK_EXCL);
  284. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  285. ip->i_d.di_dmevmask = evmask;
  286. ip->i_d.di_dmstate = state;
  287. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  288. error = xfs_trans_commit(tp);
  289. return error;
  290. }
  291. STATIC int
  292. xfs_fssetdm_by_handle(
  293. struct file *parfilp,
  294. void __user *arg)
  295. {
  296. int error;
  297. struct fsdmidata fsd;
  298. xfs_fsop_setdm_handlereq_t dmhreq;
  299. struct dentry *dentry;
  300. if (!capable(CAP_MKNOD))
  301. return -EPERM;
  302. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  303. return -EFAULT;
  304. error = mnt_want_write_file(parfilp);
  305. if (error)
  306. return error;
  307. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  308. if (IS_ERR(dentry)) {
  309. mnt_drop_write_file(parfilp);
  310. return PTR_ERR(dentry);
  311. }
  312. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  313. error = -EPERM;
  314. goto out;
  315. }
  316. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  317. error = -EFAULT;
  318. goto out;
  319. }
  320. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  321. fsd.fsd_dmstate);
  322. out:
  323. mnt_drop_write_file(parfilp);
  324. dput(dentry);
  325. return error;
  326. }
  327. STATIC int
  328. xfs_attrlist_by_handle(
  329. struct file *parfilp,
  330. void __user *arg)
  331. {
  332. int error = -ENOMEM;
  333. attrlist_cursor_kern_t *cursor;
  334. xfs_fsop_attrlist_handlereq_t al_hreq;
  335. struct dentry *dentry;
  336. char *kbuf;
  337. if (!capable(CAP_SYS_ADMIN))
  338. return -EPERM;
  339. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  340. return -EFAULT;
  341. if (al_hreq.buflen < sizeof(struct attrlist) ||
  342. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  343. return -EINVAL;
  344. /*
  345. * Reject flags, only allow namespaces.
  346. */
  347. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  348. return -EINVAL;
  349. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  350. if (IS_ERR(dentry))
  351. return PTR_ERR(dentry);
  352. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  353. if (!kbuf)
  354. goto out_dput;
  355. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  356. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  357. al_hreq.flags, cursor);
  358. if (error)
  359. goto out_kfree;
  360. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  361. error = -EFAULT;
  362. out_kfree:
  363. kmem_free(kbuf);
  364. out_dput:
  365. dput(dentry);
  366. return error;
  367. }
  368. int
  369. xfs_attrmulti_attr_get(
  370. struct inode *inode,
  371. unsigned char *name,
  372. unsigned char __user *ubuf,
  373. __uint32_t *len,
  374. __uint32_t flags)
  375. {
  376. unsigned char *kbuf;
  377. int error = -EFAULT;
  378. if (*len > XFS_XATTR_SIZE_MAX)
  379. return -EINVAL;
  380. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  381. if (!kbuf)
  382. return -ENOMEM;
  383. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  384. if (error)
  385. goto out_kfree;
  386. if (copy_to_user(ubuf, kbuf, *len))
  387. error = -EFAULT;
  388. out_kfree:
  389. kmem_free(kbuf);
  390. return error;
  391. }
  392. int
  393. xfs_attrmulti_attr_set(
  394. struct inode *inode,
  395. unsigned char *name,
  396. const unsigned char __user *ubuf,
  397. __uint32_t len,
  398. __uint32_t flags)
  399. {
  400. unsigned char *kbuf;
  401. int error;
  402. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  403. return -EPERM;
  404. if (len > XFS_XATTR_SIZE_MAX)
  405. return -EINVAL;
  406. kbuf = memdup_user(ubuf, len);
  407. if (IS_ERR(kbuf))
  408. return PTR_ERR(kbuf);
  409. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  410. if (!error)
  411. xfs_forget_acl(inode, name, flags);
  412. kfree(kbuf);
  413. return error;
  414. }
  415. int
  416. xfs_attrmulti_attr_remove(
  417. struct inode *inode,
  418. unsigned char *name,
  419. __uint32_t flags)
  420. {
  421. int error;
  422. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  423. return -EPERM;
  424. error = xfs_attr_remove(XFS_I(inode), name, flags);
  425. if (!error)
  426. xfs_forget_acl(inode, name, flags);
  427. return error;
  428. }
  429. STATIC int
  430. xfs_attrmulti_by_handle(
  431. struct file *parfilp,
  432. void __user *arg)
  433. {
  434. int error;
  435. xfs_attr_multiop_t *ops;
  436. xfs_fsop_attrmulti_handlereq_t am_hreq;
  437. struct dentry *dentry;
  438. unsigned int i, size;
  439. unsigned char *attr_name;
  440. if (!capable(CAP_SYS_ADMIN))
  441. return -EPERM;
  442. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  443. return -EFAULT;
  444. /* overflow check */
  445. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  446. return -E2BIG;
  447. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  448. if (IS_ERR(dentry))
  449. return PTR_ERR(dentry);
  450. error = -E2BIG;
  451. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  452. if (!size || size > 16 * PAGE_SIZE)
  453. goto out_dput;
  454. ops = memdup_user(am_hreq.ops, size);
  455. if (IS_ERR(ops)) {
  456. error = PTR_ERR(ops);
  457. goto out_dput;
  458. }
  459. error = -ENOMEM;
  460. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  461. if (!attr_name)
  462. goto out_kfree_ops;
  463. error = 0;
  464. for (i = 0; i < am_hreq.opcount; i++) {
  465. ops[i].am_error = strncpy_from_user((char *)attr_name,
  466. ops[i].am_attrname, MAXNAMELEN);
  467. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  468. error = -ERANGE;
  469. if (ops[i].am_error < 0)
  470. break;
  471. switch (ops[i].am_opcode) {
  472. case ATTR_OP_GET:
  473. ops[i].am_error = xfs_attrmulti_attr_get(
  474. d_inode(dentry), attr_name,
  475. ops[i].am_attrvalue, &ops[i].am_length,
  476. ops[i].am_flags);
  477. break;
  478. case ATTR_OP_SET:
  479. ops[i].am_error = mnt_want_write_file(parfilp);
  480. if (ops[i].am_error)
  481. break;
  482. ops[i].am_error = xfs_attrmulti_attr_set(
  483. d_inode(dentry), attr_name,
  484. ops[i].am_attrvalue, ops[i].am_length,
  485. ops[i].am_flags);
  486. mnt_drop_write_file(parfilp);
  487. break;
  488. case ATTR_OP_REMOVE:
  489. ops[i].am_error = mnt_want_write_file(parfilp);
  490. if (ops[i].am_error)
  491. break;
  492. ops[i].am_error = xfs_attrmulti_attr_remove(
  493. d_inode(dentry), attr_name,
  494. ops[i].am_flags);
  495. mnt_drop_write_file(parfilp);
  496. break;
  497. default:
  498. ops[i].am_error = -EINVAL;
  499. }
  500. }
  501. if (copy_to_user(am_hreq.ops, ops, size))
  502. error = -EFAULT;
  503. kfree(attr_name);
  504. out_kfree_ops:
  505. kfree(ops);
  506. out_dput:
  507. dput(dentry);
  508. return error;
  509. }
  510. int
  511. xfs_ioc_space(
  512. struct xfs_inode *ip,
  513. struct inode *inode,
  514. struct file *filp,
  515. int ioflags,
  516. unsigned int cmd,
  517. xfs_flock64_t *bf)
  518. {
  519. struct iattr iattr;
  520. enum xfs_prealloc_flags flags = 0;
  521. uint iolock = XFS_IOLOCK_EXCL;
  522. int error;
  523. /*
  524. * Only allow the sys admin to reserve space unless
  525. * unwritten extents are enabled.
  526. */
  527. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  528. !capable(CAP_SYS_ADMIN))
  529. return -EPERM;
  530. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  531. return -EPERM;
  532. if (!(filp->f_mode & FMODE_WRITE))
  533. return -EBADF;
  534. if (!S_ISREG(inode->i_mode))
  535. return -EINVAL;
  536. if (filp->f_flags & O_DSYNC)
  537. flags |= XFS_PREALLOC_SYNC;
  538. if (ioflags & XFS_IO_INVIS)
  539. flags |= XFS_PREALLOC_INVISIBLE;
  540. error = mnt_want_write_file(filp);
  541. if (error)
  542. return error;
  543. xfs_ilock(ip, iolock);
  544. error = xfs_break_layouts(inode, &iolock, false);
  545. if (error)
  546. goto out_unlock;
  547. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  548. iolock |= XFS_MMAPLOCK_EXCL;
  549. switch (bf->l_whence) {
  550. case 0: /*SEEK_SET*/
  551. break;
  552. case 1: /*SEEK_CUR*/
  553. bf->l_start += filp->f_pos;
  554. break;
  555. case 2: /*SEEK_END*/
  556. bf->l_start += XFS_ISIZE(ip);
  557. break;
  558. default:
  559. error = -EINVAL;
  560. goto out_unlock;
  561. }
  562. /*
  563. * length of <= 0 for resv/unresv/zero is invalid. length for
  564. * alloc/free is ignored completely and we have no idea what userspace
  565. * might have set it to, so set it to zero to allow range
  566. * checks to pass.
  567. */
  568. switch (cmd) {
  569. case XFS_IOC_ZERO_RANGE:
  570. case XFS_IOC_RESVSP:
  571. case XFS_IOC_RESVSP64:
  572. case XFS_IOC_UNRESVSP:
  573. case XFS_IOC_UNRESVSP64:
  574. if (bf->l_len <= 0) {
  575. error = -EINVAL;
  576. goto out_unlock;
  577. }
  578. break;
  579. default:
  580. bf->l_len = 0;
  581. break;
  582. }
  583. if (bf->l_start < 0 ||
  584. bf->l_start > inode->i_sb->s_maxbytes ||
  585. bf->l_start + bf->l_len < 0 ||
  586. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  587. error = -EINVAL;
  588. goto out_unlock;
  589. }
  590. switch (cmd) {
  591. case XFS_IOC_ZERO_RANGE:
  592. flags |= XFS_PREALLOC_SET;
  593. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  594. break;
  595. case XFS_IOC_RESVSP:
  596. case XFS_IOC_RESVSP64:
  597. flags |= XFS_PREALLOC_SET;
  598. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  599. XFS_BMAPI_PREALLOC);
  600. break;
  601. case XFS_IOC_UNRESVSP:
  602. case XFS_IOC_UNRESVSP64:
  603. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  604. break;
  605. case XFS_IOC_ALLOCSP:
  606. case XFS_IOC_ALLOCSP64:
  607. case XFS_IOC_FREESP:
  608. case XFS_IOC_FREESP64:
  609. flags |= XFS_PREALLOC_CLEAR;
  610. if (bf->l_start > XFS_ISIZE(ip)) {
  611. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  612. bf->l_start - XFS_ISIZE(ip), 0);
  613. if (error)
  614. goto out_unlock;
  615. }
  616. iattr.ia_valid = ATTR_SIZE;
  617. iattr.ia_size = bf->l_start;
  618. error = xfs_setattr_size(ip, &iattr);
  619. break;
  620. default:
  621. ASSERT(0);
  622. error = -EINVAL;
  623. }
  624. if (error)
  625. goto out_unlock;
  626. error = xfs_update_prealloc_flags(ip, flags);
  627. out_unlock:
  628. xfs_iunlock(ip, iolock);
  629. mnt_drop_write_file(filp);
  630. return error;
  631. }
  632. STATIC int
  633. xfs_ioc_bulkstat(
  634. xfs_mount_t *mp,
  635. unsigned int cmd,
  636. void __user *arg)
  637. {
  638. xfs_fsop_bulkreq_t bulkreq;
  639. int count; /* # of records returned */
  640. xfs_ino_t inlast; /* last inode number */
  641. int done;
  642. int error;
  643. /* done = 1 if there are more stats to get and if bulkstat */
  644. /* should be called again (unused here, but used in dmapi) */
  645. if (!capable(CAP_SYS_ADMIN))
  646. return -EPERM;
  647. if (XFS_FORCED_SHUTDOWN(mp))
  648. return -EIO;
  649. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  650. return -EFAULT;
  651. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  652. return -EFAULT;
  653. if ((count = bulkreq.icount) <= 0)
  654. return -EINVAL;
  655. if (bulkreq.ubuffer == NULL)
  656. return -EINVAL;
  657. if (cmd == XFS_IOC_FSINUMBERS)
  658. error = xfs_inumbers(mp, &inlast, &count,
  659. bulkreq.ubuffer, xfs_inumbers_fmt);
  660. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  661. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  662. sizeof(xfs_bstat_t), NULL, &done);
  663. else /* XFS_IOC_FSBULKSTAT */
  664. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  665. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  666. &done);
  667. if (error)
  668. return error;
  669. if (bulkreq.ocount != NULL) {
  670. if (copy_to_user(bulkreq.lastip, &inlast,
  671. sizeof(xfs_ino_t)))
  672. return -EFAULT;
  673. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  674. return -EFAULT;
  675. }
  676. return 0;
  677. }
  678. STATIC int
  679. xfs_ioc_fsgeometry_v1(
  680. xfs_mount_t *mp,
  681. void __user *arg)
  682. {
  683. xfs_fsop_geom_t fsgeo;
  684. int error;
  685. error = xfs_fs_geometry(mp, &fsgeo, 3);
  686. if (error)
  687. return error;
  688. /*
  689. * Caller should have passed an argument of type
  690. * xfs_fsop_geom_v1_t. This is a proper subset of the
  691. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  692. */
  693. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  694. return -EFAULT;
  695. return 0;
  696. }
  697. STATIC int
  698. xfs_ioc_fsgeometry(
  699. xfs_mount_t *mp,
  700. void __user *arg)
  701. {
  702. xfs_fsop_geom_t fsgeo;
  703. int error;
  704. error = xfs_fs_geometry(mp, &fsgeo, 4);
  705. if (error)
  706. return error;
  707. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  708. return -EFAULT;
  709. return 0;
  710. }
  711. /*
  712. * Linux extended inode flags interface.
  713. */
  714. STATIC unsigned int
  715. xfs_merge_ioc_xflags(
  716. unsigned int flags,
  717. unsigned int start)
  718. {
  719. unsigned int xflags = start;
  720. if (flags & FS_IMMUTABLE_FL)
  721. xflags |= FS_XFLAG_IMMUTABLE;
  722. else
  723. xflags &= ~FS_XFLAG_IMMUTABLE;
  724. if (flags & FS_APPEND_FL)
  725. xflags |= FS_XFLAG_APPEND;
  726. else
  727. xflags &= ~FS_XFLAG_APPEND;
  728. if (flags & FS_SYNC_FL)
  729. xflags |= FS_XFLAG_SYNC;
  730. else
  731. xflags &= ~FS_XFLAG_SYNC;
  732. if (flags & FS_NOATIME_FL)
  733. xflags |= FS_XFLAG_NOATIME;
  734. else
  735. xflags &= ~FS_XFLAG_NOATIME;
  736. if (flags & FS_NODUMP_FL)
  737. xflags |= FS_XFLAG_NODUMP;
  738. else
  739. xflags &= ~FS_XFLAG_NODUMP;
  740. return xflags;
  741. }
  742. STATIC unsigned int
  743. xfs_di2lxflags(
  744. __uint16_t di_flags)
  745. {
  746. unsigned int flags = 0;
  747. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  748. flags |= FS_IMMUTABLE_FL;
  749. if (di_flags & XFS_DIFLAG_APPEND)
  750. flags |= FS_APPEND_FL;
  751. if (di_flags & XFS_DIFLAG_SYNC)
  752. flags |= FS_SYNC_FL;
  753. if (di_flags & XFS_DIFLAG_NOATIME)
  754. flags |= FS_NOATIME_FL;
  755. if (di_flags & XFS_DIFLAG_NODUMP)
  756. flags |= FS_NODUMP_FL;
  757. return flags;
  758. }
  759. STATIC int
  760. xfs_ioc_fsgetxattr(
  761. xfs_inode_t *ip,
  762. int attr,
  763. void __user *arg)
  764. {
  765. struct fsxattr fa;
  766. memset(&fa, 0, sizeof(struct fsxattr));
  767. xfs_ilock(ip, XFS_ILOCK_SHARED);
  768. fa.fsx_xflags = xfs_ip2xflags(ip);
  769. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  770. fa.fsx_projid = xfs_get_projid(ip);
  771. if (attr) {
  772. if (ip->i_afp) {
  773. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  774. fa.fsx_nextents = ip->i_afp->if_bytes /
  775. sizeof(xfs_bmbt_rec_t);
  776. else
  777. fa.fsx_nextents = ip->i_d.di_anextents;
  778. } else
  779. fa.fsx_nextents = 0;
  780. } else {
  781. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  782. fa.fsx_nextents = ip->i_df.if_bytes /
  783. sizeof(xfs_bmbt_rec_t);
  784. else
  785. fa.fsx_nextents = ip->i_d.di_nextents;
  786. }
  787. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  788. if (copy_to_user(arg, &fa, sizeof(fa)))
  789. return -EFAULT;
  790. return 0;
  791. }
  792. STATIC void
  793. xfs_set_diflags(
  794. struct xfs_inode *ip,
  795. unsigned int xflags)
  796. {
  797. unsigned int di_flags;
  798. uint64_t di_flags2;
  799. /* can't set PREALLOC this way, just preserve it */
  800. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  801. if (xflags & FS_XFLAG_IMMUTABLE)
  802. di_flags |= XFS_DIFLAG_IMMUTABLE;
  803. if (xflags & FS_XFLAG_APPEND)
  804. di_flags |= XFS_DIFLAG_APPEND;
  805. if (xflags & FS_XFLAG_SYNC)
  806. di_flags |= XFS_DIFLAG_SYNC;
  807. if (xflags & FS_XFLAG_NOATIME)
  808. di_flags |= XFS_DIFLAG_NOATIME;
  809. if (xflags & FS_XFLAG_NODUMP)
  810. di_flags |= XFS_DIFLAG_NODUMP;
  811. if (xflags & FS_XFLAG_NODEFRAG)
  812. di_flags |= XFS_DIFLAG_NODEFRAG;
  813. if (xflags & FS_XFLAG_FILESTREAM)
  814. di_flags |= XFS_DIFLAG_FILESTREAM;
  815. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  816. if (xflags & FS_XFLAG_RTINHERIT)
  817. di_flags |= XFS_DIFLAG_RTINHERIT;
  818. if (xflags & FS_XFLAG_NOSYMLINKS)
  819. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  820. if (xflags & FS_XFLAG_EXTSZINHERIT)
  821. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  822. if (xflags & FS_XFLAG_PROJINHERIT)
  823. di_flags |= XFS_DIFLAG_PROJINHERIT;
  824. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  825. if (xflags & FS_XFLAG_REALTIME)
  826. di_flags |= XFS_DIFLAG_REALTIME;
  827. if (xflags & FS_XFLAG_EXTSIZE)
  828. di_flags |= XFS_DIFLAG_EXTSIZE;
  829. }
  830. ip->i_d.di_flags = di_flags;
  831. /* diflags2 only valid for v3 inodes. */
  832. if (ip->i_d.di_version < 3)
  833. return;
  834. di_flags2 = 0;
  835. if (xflags & FS_XFLAG_DAX)
  836. di_flags2 |= XFS_DIFLAG2_DAX;
  837. ip->i_d.di_flags2 = di_flags2;
  838. }
  839. STATIC void
  840. xfs_diflags_to_linux(
  841. struct xfs_inode *ip)
  842. {
  843. struct inode *inode = VFS_I(ip);
  844. unsigned int xflags = xfs_ip2xflags(ip);
  845. if (xflags & FS_XFLAG_IMMUTABLE)
  846. inode->i_flags |= S_IMMUTABLE;
  847. else
  848. inode->i_flags &= ~S_IMMUTABLE;
  849. if (xflags & FS_XFLAG_APPEND)
  850. inode->i_flags |= S_APPEND;
  851. else
  852. inode->i_flags &= ~S_APPEND;
  853. if (xflags & FS_XFLAG_SYNC)
  854. inode->i_flags |= S_SYNC;
  855. else
  856. inode->i_flags &= ~S_SYNC;
  857. if (xflags & FS_XFLAG_NOATIME)
  858. inode->i_flags |= S_NOATIME;
  859. else
  860. inode->i_flags &= ~S_NOATIME;
  861. if (xflags & FS_XFLAG_DAX)
  862. inode->i_flags |= S_DAX;
  863. else
  864. inode->i_flags &= ~S_DAX;
  865. }
  866. static int
  867. xfs_ioctl_setattr_xflags(
  868. struct xfs_trans *tp,
  869. struct xfs_inode *ip,
  870. struct fsxattr *fa)
  871. {
  872. struct xfs_mount *mp = ip->i_mount;
  873. /* Can't change realtime flag if any extents are allocated. */
  874. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  875. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  876. return -EINVAL;
  877. /* If realtime flag is set then must have realtime device */
  878. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  879. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  880. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  881. return -EINVAL;
  882. }
  883. /*
  884. * Can't modify an immutable/append-only file unless
  885. * we have appropriate permission.
  886. */
  887. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  888. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  889. !capable(CAP_LINUX_IMMUTABLE))
  890. return -EPERM;
  891. xfs_set_diflags(ip, fa->fsx_xflags);
  892. xfs_diflags_to_linux(ip);
  893. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  894. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  895. XFS_STATS_INC(mp, xs_ig_attrchg);
  896. return 0;
  897. }
  898. /*
  899. * If we are changing DAX flags, we have to ensure the file is clean and any
  900. * cached objects in the address space are invalidated and removed. This
  901. * requires us to lock out other IO and page faults similar to a truncate
  902. * operation. The locks need to be held until the transaction has been committed
  903. * so that the cache invalidation is atomic with respect to the DAX flag
  904. * manipulation.
  905. */
  906. static int
  907. xfs_ioctl_setattr_dax_invalidate(
  908. struct xfs_inode *ip,
  909. struct fsxattr *fa,
  910. int *join_flags)
  911. {
  912. struct inode *inode = VFS_I(ip);
  913. int error;
  914. *join_flags = 0;
  915. /*
  916. * It is only valid to set the DAX flag on regular files and
  917. * directories on filesystems where the block size is equal to the page
  918. * size. On directories it serves as an inherit hint.
  919. */
  920. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  921. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  922. return -EINVAL;
  923. if (ip->i_mount->m_sb.sb_blocksize != PAGE_SIZE)
  924. return -EINVAL;
  925. }
  926. /* If the DAX state is not changing, we have nothing to do here. */
  927. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  928. return 0;
  929. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  930. return 0;
  931. /* lock, flush and invalidate mapping in preparation for flag change */
  932. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  933. error = filemap_write_and_wait(inode->i_mapping);
  934. if (error)
  935. goto out_unlock;
  936. error = invalidate_inode_pages2(inode->i_mapping);
  937. if (error)
  938. goto out_unlock;
  939. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  940. return 0;
  941. out_unlock:
  942. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  943. return error;
  944. }
  945. /*
  946. * Set up the transaction structure for the setattr operation, checking that we
  947. * have permission to do so. On success, return a clean transaction and the
  948. * inode locked exclusively ready for further operation specific checks. On
  949. * failure, return an error without modifying or locking the inode.
  950. *
  951. * The inode might already be IO locked on call. If this is the case, it is
  952. * indicated in @join_flags and we take full responsibility for ensuring they
  953. * are unlocked from now on. Hence if we have an error here, we still have to
  954. * unlock them. Otherwise, once they are joined to the transaction, they will
  955. * be unlocked on commit/cancel.
  956. */
  957. static struct xfs_trans *
  958. xfs_ioctl_setattr_get_trans(
  959. struct xfs_inode *ip,
  960. int join_flags)
  961. {
  962. struct xfs_mount *mp = ip->i_mount;
  963. struct xfs_trans *tp;
  964. int error = -EROFS;
  965. if (mp->m_flags & XFS_MOUNT_RDONLY)
  966. goto out_unlock;
  967. error = -EIO;
  968. if (XFS_FORCED_SHUTDOWN(mp))
  969. goto out_unlock;
  970. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  971. if (error)
  972. return ERR_PTR(error);
  973. xfs_ilock(ip, XFS_ILOCK_EXCL);
  974. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  975. join_flags = 0;
  976. /*
  977. * CAP_FOWNER overrides the following restrictions:
  978. *
  979. * The user ID of the calling process must be equal to the file owner
  980. * ID, except in cases where the CAP_FSETID capability is applicable.
  981. */
  982. if (!inode_owner_or_capable(VFS_I(ip))) {
  983. error = -EPERM;
  984. goto out_cancel;
  985. }
  986. if (mp->m_flags & XFS_MOUNT_WSYNC)
  987. xfs_trans_set_sync(tp);
  988. return tp;
  989. out_cancel:
  990. xfs_trans_cancel(tp);
  991. out_unlock:
  992. if (join_flags)
  993. xfs_iunlock(ip, join_flags);
  994. return ERR_PTR(error);
  995. }
  996. /*
  997. * extent size hint validation is somewhat cumbersome. Rules are:
  998. *
  999. * 1. extent size hint is only valid for directories and regular files
  1000. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1001. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1002. * 4. can only be changed on regular files if no extents are allocated
  1003. * 5. can be changed on directories at any time
  1004. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1005. * 7. Extent size must be a multiple of the appropriate block size.
  1006. * 8. for non-realtime files, the extent size hint must be limited
  1007. * to half the AG size to avoid alignment extending the extent beyond the
  1008. * limits of the AG.
  1009. */
  1010. static int
  1011. xfs_ioctl_setattr_check_extsize(
  1012. struct xfs_inode *ip,
  1013. struct fsxattr *fa)
  1014. {
  1015. struct xfs_mount *mp = ip->i_mount;
  1016. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1017. return -EINVAL;
  1018. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1019. !S_ISDIR(VFS_I(ip)->i_mode))
  1020. return -EINVAL;
  1021. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1022. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1023. return -EINVAL;
  1024. if (fa->fsx_extsize != 0) {
  1025. xfs_extlen_t size;
  1026. xfs_fsblock_t extsize_fsb;
  1027. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1028. if (extsize_fsb > MAXEXTLEN)
  1029. return -EINVAL;
  1030. if (XFS_IS_REALTIME_INODE(ip) ||
  1031. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1032. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1033. } else {
  1034. size = mp->m_sb.sb_blocksize;
  1035. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1036. return -EINVAL;
  1037. }
  1038. if (fa->fsx_extsize % size)
  1039. return -EINVAL;
  1040. } else
  1041. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1042. return 0;
  1043. }
  1044. static int
  1045. xfs_ioctl_setattr_check_projid(
  1046. struct xfs_inode *ip,
  1047. struct fsxattr *fa)
  1048. {
  1049. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1050. if (fa->fsx_projid > (__uint16_t)-1 &&
  1051. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1052. return -EINVAL;
  1053. /*
  1054. * Project Quota ID state is only allowed to change from within the init
  1055. * namespace. Enforce that restriction only if we are trying to change
  1056. * the quota ID state. Everything else is allowed in user namespaces.
  1057. */
  1058. if (current_user_ns() == &init_user_ns)
  1059. return 0;
  1060. if (xfs_get_projid(ip) != fa->fsx_projid)
  1061. return -EINVAL;
  1062. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1063. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1064. return -EINVAL;
  1065. return 0;
  1066. }
  1067. STATIC int
  1068. xfs_ioctl_setattr(
  1069. xfs_inode_t *ip,
  1070. struct fsxattr *fa)
  1071. {
  1072. struct xfs_mount *mp = ip->i_mount;
  1073. struct xfs_trans *tp;
  1074. struct xfs_dquot *udqp = NULL;
  1075. struct xfs_dquot *pdqp = NULL;
  1076. struct xfs_dquot *olddquot = NULL;
  1077. int code;
  1078. int join_flags = 0;
  1079. trace_xfs_ioctl_setattr(ip);
  1080. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1081. if (code)
  1082. return code;
  1083. /*
  1084. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1085. * before we start any other transactions. Trying to do this later
  1086. * is messy. We don't care to take a readlock to look at the ids
  1087. * in inode here, because we can't hold it across the trans_reserve.
  1088. * If the IDs do change before we take the ilock, we're covered
  1089. * because the i_*dquot fields will get updated anyway.
  1090. */
  1091. if (XFS_IS_QUOTA_ON(mp)) {
  1092. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1093. ip->i_d.di_gid, fa->fsx_projid,
  1094. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1095. if (code)
  1096. return code;
  1097. }
  1098. /*
  1099. * Changing DAX config may require inode locking for mapping
  1100. * invalidation. These need to be held all the way to transaction commit
  1101. * or cancel time, so need to be passed through to
  1102. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1103. * appropriately.
  1104. */
  1105. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1106. if (code)
  1107. goto error_free_dquots;
  1108. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1109. if (IS_ERR(tp)) {
  1110. code = PTR_ERR(tp);
  1111. goto error_free_dquots;
  1112. }
  1113. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1114. xfs_get_projid(ip) != fa->fsx_projid) {
  1115. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1116. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1117. if (code) /* out of quota */
  1118. goto error_trans_cancel;
  1119. }
  1120. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1121. if (code)
  1122. goto error_trans_cancel;
  1123. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1124. if (code)
  1125. goto error_trans_cancel;
  1126. /*
  1127. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1128. * overrides the following restrictions:
  1129. *
  1130. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1131. * successful return from chown()
  1132. */
  1133. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1134. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1135. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1136. /* Change the ownerships and register project quota modifications */
  1137. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1138. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1139. olddquot = xfs_qm_vop_chown(tp, ip,
  1140. &ip->i_pdquot, pdqp);
  1141. }
  1142. ASSERT(ip->i_d.di_version > 1);
  1143. xfs_set_projid(ip, fa->fsx_projid);
  1144. }
  1145. /*
  1146. * Only set the extent size hint if we've already determined that the
  1147. * extent size hint should be set on the inode. If no extent size flags
  1148. * are set on the inode then unconditionally clear the extent size hint.
  1149. */
  1150. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1151. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1152. else
  1153. ip->i_d.di_extsize = 0;
  1154. code = xfs_trans_commit(tp);
  1155. /*
  1156. * Release any dquot(s) the inode had kept before chown.
  1157. */
  1158. xfs_qm_dqrele(olddquot);
  1159. xfs_qm_dqrele(udqp);
  1160. xfs_qm_dqrele(pdqp);
  1161. return code;
  1162. error_trans_cancel:
  1163. xfs_trans_cancel(tp);
  1164. error_free_dquots:
  1165. xfs_qm_dqrele(udqp);
  1166. xfs_qm_dqrele(pdqp);
  1167. return code;
  1168. }
  1169. STATIC int
  1170. xfs_ioc_fssetxattr(
  1171. xfs_inode_t *ip,
  1172. struct file *filp,
  1173. void __user *arg)
  1174. {
  1175. struct fsxattr fa;
  1176. int error;
  1177. if (copy_from_user(&fa, arg, sizeof(fa)))
  1178. return -EFAULT;
  1179. error = mnt_want_write_file(filp);
  1180. if (error)
  1181. return error;
  1182. error = xfs_ioctl_setattr(ip, &fa);
  1183. mnt_drop_write_file(filp);
  1184. return error;
  1185. }
  1186. STATIC int
  1187. xfs_ioc_getxflags(
  1188. xfs_inode_t *ip,
  1189. void __user *arg)
  1190. {
  1191. unsigned int flags;
  1192. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1193. if (copy_to_user(arg, &flags, sizeof(flags)))
  1194. return -EFAULT;
  1195. return 0;
  1196. }
  1197. STATIC int
  1198. xfs_ioc_setxflags(
  1199. struct xfs_inode *ip,
  1200. struct file *filp,
  1201. void __user *arg)
  1202. {
  1203. struct xfs_trans *tp;
  1204. struct fsxattr fa;
  1205. unsigned int flags;
  1206. int join_flags = 0;
  1207. int error;
  1208. if (copy_from_user(&flags, arg, sizeof(flags)))
  1209. return -EFAULT;
  1210. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1211. FS_NOATIME_FL | FS_NODUMP_FL | \
  1212. FS_SYNC_FL))
  1213. return -EOPNOTSUPP;
  1214. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1215. error = mnt_want_write_file(filp);
  1216. if (error)
  1217. return error;
  1218. /*
  1219. * Changing DAX config may require inode locking for mapping
  1220. * invalidation. These need to be held all the way to transaction commit
  1221. * or cancel time, so need to be passed through to
  1222. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1223. * appropriately.
  1224. */
  1225. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1226. if (error)
  1227. goto out_drop_write;
  1228. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1229. if (IS_ERR(tp)) {
  1230. error = PTR_ERR(tp);
  1231. goto out_drop_write;
  1232. }
  1233. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1234. if (error) {
  1235. xfs_trans_cancel(tp);
  1236. goto out_drop_write;
  1237. }
  1238. error = xfs_trans_commit(tp);
  1239. out_drop_write:
  1240. mnt_drop_write_file(filp);
  1241. return error;
  1242. }
  1243. STATIC int
  1244. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1245. {
  1246. struct getbmap __user *base = (struct getbmap __user *)*ap;
  1247. /* copy only getbmap portion (not getbmapx) */
  1248. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1249. return -EFAULT;
  1250. *ap += sizeof(struct getbmap);
  1251. return 0;
  1252. }
  1253. STATIC int
  1254. xfs_ioc_getbmap(
  1255. struct xfs_inode *ip,
  1256. int ioflags,
  1257. unsigned int cmd,
  1258. void __user *arg)
  1259. {
  1260. struct getbmapx bmx;
  1261. int error;
  1262. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1263. return -EFAULT;
  1264. if (bmx.bmv_count < 2)
  1265. return -EINVAL;
  1266. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1267. if (ioflags & XFS_IO_INVIS)
  1268. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1269. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1270. (__force struct getbmap *)arg+1);
  1271. if (error)
  1272. return error;
  1273. /* copy back header - only size of getbmap */
  1274. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1275. return -EFAULT;
  1276. return 0;
  1277. }
  1278. STATIC int
  1279. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1280. {
  1281. struct getbmapx __user *base = (struct getbmapx __user *)*ap;
  1282. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1283. return -EFAULT;
  1284. *ap += sizeof(struct getbmapx);
  1285. return 0;
  1286. }
  1287. STATIC int
  1288. xfs_ioc_getbmapx(
  1289. struct xfs_inode *ip,
  1290. void __user *arg)
  1291. {
  1292. struct getbmapx bmx;
  1293. int error;
  1294. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1295. return -EFAULT;
  1296. if (bmx.bmv_count < 2)
  1297. return -EINVAL;
  1298. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1299. return -EINVAL;
  1300. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1301. (__force struct getbmapx *)arg+1);
  1302. if (error)
  1303. return error;
  1304. /* copy back header */
  1305. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1306. return -EFAULT;
  1307. return 0;
  1308. }
  1309. int
  1310. xfs_ioc_swapext(
  1311. xfs_swapext_t *sxp)
  1312. {
  1313. xfs_inode_t *ip, *tip;
  1314. struct fd f, tmp;
  1315. int error = 0;
  1316. /* Pull information for the target fd */
  1317. f = fdget((int)sxp->sx_fdtarget);
  1318. if (!f.file) {
  1319. error = -EINVAL;
  1320. goto out;
  1321. }
  1322. if (!(f.file->f_mode & FMODE_WRITE) ||
  1323. !(f.file->f_mode & FMODE_READ) ||
  1324. (f.file->f_flags & O_APPEND)) {
  1325. error = -EBADF;
  1326. goto out_put_file;
  1327. }
  1328. tmp = fdget((int)sxp->sx_fdtmp);
  1329. if (!tmp.file) {
  1330. error = -EINVAL;
  1331. goto out_put_file;
  1332. }
  1333. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1334. !(tmp.file->f_mode & FMODE_READ) ||
  1335. (tmp.file->f_flags & O_APPEND)) {
  1336. error = -EBADF;
  1337. goto out_put_tmp_file;
  1338. }
  1339. if (IS_SWAPFILE(file_inode(f.file)) ||
  1340. IS_SWAPFILE(file_inode(tmp.file))) {
  1341. error = -EINVAL;
  1342. goto out_put_tmp_file;
  1343. }
  1344. ip = XFS_I(file_inode(f.file));
  1345. tip = XFS_I(file_inode(tmp.file));
  1346. if (ip->i_mount != tip->i_mount) {
  1347. error = -EINVAL;
  1348. goto out_put_tmp_file;
  1349. }
  1350. if (ip->i_ino == tip->i_ino) {
  1351. error = -EINVAL;
  1352. goto out_put_tmp_file;
  1353. }
  1354. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1355. error = -EIO;
  1356. goto out_put_tmp_file;
  1357. }
  1358. error = xfs_swap_extents(ip, tip, sxp);
  1359. out_put_tmp_file:
  1360. fdput(tmp);
  1361. out_put_file:
  1362. fdput(f);
  1363. out:
  1364. return error;
  1365. }
  1366. /*
  1367. * Note: some of the ioctl's return positive numbers as a
  1368. * byte count indicating success, such as readlink_by_handle.
  1369. * So we don't "sign flip" like most other routines. This means
  1370. * true errors need to be returned as a negative value.
  1371. */
  1372. long
  1373. xfs_file_ioctl(
  1374. struct file *filp,
  1375. unsigned int cmd,
  1376. unsigned long p)
  1377. {
  1378. struct inode *inode = file_inode(filp);
  1379. struct xfs_inode *ip = XFS_I(inode);
  1380. struct xfs_mount *mp = ip->i_mount;
  1381. void __user *arg = (void __user *)p;
  1382. int ioflags = 0;
  1383. int error;
  1384. if (filp->f_mode & FMODE_NOCMTIME)
  1385. ioflags |= XFS_IO_INVIS;
  1386. trace_xfs_file_ioctl(ip);
  1387. switch (cmd) {
  1388. case FITRIM:
  1389. return xfs_ioc_trim(mp, arg);
  1390. case XFS_IOC_ALLOCSP:
  1391. case XFS_IOC_FREESP:
  1392. case XFS_IOC_RESVSP:
  1393. case XFS_IOC_UNRESVSP:
  1394. case XFS_IOC_ALLOCSP64:
  1395. case XFS_IOC_FREESP64:
  1396. case XFS_IOC_RESVSP64:
  1397. case XFS_IOC_UNRESVSP64:
  1398. case XFS_IOC_ZERO_RANGE: {
  1399. xfs_flock64_t bf;
  1400. if (copy_from_user(&bf, arg, sizeof(bf)))
  1401. return -EFAULT;
  1402. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1403. }
  1404. case XFS_IOC_DIOINFO: {
  1405. struct dioattr da;
  1406. xfs_buftarg_t *target =
  1407. XFS_IS_REALTIME_INODE(ip) ?
  1408. mp->m_rtdev_targp : mp->m_ddev_targp;
  1409. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1410. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1411. if (copy_to_user(arg, &da, sizeof(da)))
  1412. return -EFAULT;
  1413. return 0;
  1414. }
  1415. case XFS_IOC_FSBULKSTAT_SINGLE:
  1416. case XFS_IOC_FSBULKSTAT:
  1417. case XFS_IOC_FSINUMBERS:
  1418. return xfs_ioc_bulkstat(mp, cmd, arg);
  1419. case XFS_IOC_FSGEOMETRY_V1:
  1420. return xfs_ioc_fsgeometry_v1(mp, arg);
  1421. case XFS_IOC_FSGEOMETRY:
  1422. return xfs_ioc_fsgeometry(mp, arg);
  1423. case XFS_IOC_GETVERSION:
  1424. return put_user(inode->i_generation, (int __user *)arg);
  1425. case XFS_IOC_FSGETXATTR:
  1426. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1427. case XFS_IOC_FSGETXATTRA:
  1428. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1429. case XFS_IOC_FSSETXATTR:
  1430. return xfs_ioc_fssetxattr(ip, filp, arg);
  1431. case XFS_IOC_GETXFLAGS:
  1432. return xfs_ioc_getxflags(ip, arg);
  1433. case XFS_IOC_SETXFLAGS:
  1434. return xfs_ioc_setxflags(ip, filp, arg);
  1435. case XFS_IOC_FSSETDM: {
  1436. struct fsdmidata dmi;
  1437. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1438. return -EFAULT;
  1439. error = mnt_want_write_file(filp);
  1440. if (error)
  1441. return error;
  1442. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1443. dmi.fsd_dmstate);
  1444. mnt_drop_write_file(filp);
  1445. return error;
  1446. }
  1447. case XFS_IOC_GETBMAP:
  1448. case XFS_IOC_GETBMAPA:
  1449. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1450. case XFS_IOC_GETBMAPX:
  1451. return xfs_ioc_getbmapx(ip, arg);
  1452. case XFS_IOC_FD_TO_HANDLE:
  1453. case XFS_IOC_PATH_TO_HANDLE:
  1454. case XFS_IOC_PATH_TO_FSHANDLE: {
  1455. xfs_fsop_handlereq_t hreq;
  1456. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1457. return -EFAULT;
  1458. return xfs_find_handle(cmd, &hreq);
  1459. }
  1460. case XFS_IOC_OPEN_BY_HANDLE: {
  1461. xfs_fsop_handlereq_t hreq;
  1462. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1463. return -EFAULT;
  1464. return xfs_open_by_handle(filp, &hreq);
  1465. }
  1466. case XFS_IOC_FSSETDM_BY_HANDLE:
  1467. return xfs_fssetdm_by_handle(filp, arg);
  1468. case XFS_IOC_READLINK_BY_HANDLE: {
  1469. xfs_fsop_handlereq_t hreq;
  1470. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1471. return -EFAULT;
  1472. return xfs_readlink_by_handle(filp, &hreq);
  1473. }
  1474. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1475. return xfs_attrlist_by_handle(filp, arg);
  1476. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1477. return xfs_attrmulti_by_handle(filp, arg);
  1478. case XFS_IOC_SWAPEXT: {
  1479. struct xfs_swapext sxp;
  1480. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1481. return -EFAULT;
  1482. error = mnt_want_write_file(filp);
  1483. if (error)
  1484. return error;
  1485. error = xfs_ioc_swapext(&sxp);
  1486. mnt_drop_write_file(filp);
  1487. return error;
  1488. }
  1489. case XFS_IOC_FSCOUNTS: {
  1490. xfs_fsop_counts_t out;
  1491. error = xfs_fs_counts(mp, &out);
  1492. if (error)
  1493. return error;
  1494. if (copy_to_user(arg, &out, sizeof(out)))
  1495. return -EFAULT;
  1496. return 0;
  1497. }
  1498. case XFS_IOC_SET_RESBLKS: {
  1499. xfs_fsop_resblks_t inout;
  1500. __uint64_t in;
  1501. if (!capable(CAP_SYS_ADMIN))
  1502. return -EPERM;
  1503. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1504. return -EROFS;
  1505. if (copy_from_user(&inout, arg, sizeof(inout)))
  1506. return -EFAULT;
  1507. error = mnt_want_write_file(filp);
  1508. if (error)
  1509. return error;
  1510. /* input parameter is passed in resblks field of structure */
  1511. in = inout.resblks;
  1512. error = xfs_reserve_blocks(mp, &in, &inout);
  1513. mnt_drop_write_file(filp);
  1514. if (error)
  1515. return error;
  1516. if (copy_to_user(arg, &inout, sizeof(inout)))
  1517. return -EFAULT;
  1518. return 0;
  1519. }
  1520. case XFS_IOC_GET_RESBLKS: {
  1521. xfs_fsop_resblks_t out;
  1522. if (!capable(CAP_SYS_ADMIN))
  1523. return -EPERM;
  1524. error = xfs_reserve_blocks(mp, NULL, &out);
  1525. if (error)
  1526. return error;
  1527. if (copy_to_user(arg, &out, sizeof(out)))
  1528. return -EFAULT;
  1529. return 0;
  1530. }
  1531. case XFS_IOC_FSGROWFSDATA: {
  1532. xfs_growfs_data_t in;
  1533. if (copy_from_user(&in, arg, sizeof(in)))
  1534. return -EFAULT;
  1535. error = mnt_want_write_file(filp);
  1536. if (error)
  1537. return error;
  1538. error = xfs_growfs_data(mp, &in);
  1539. mnt_drop_write_file(filp);
  1540. return error;
  1541. }
  1542. case XFS_IOC_FSGROWFSLOG: {
  1543. xfs_growfs_log_t in;
  1544. if (copy_from_user(&in, arg, sizeof(in)))
  1545. return -EFAULT;
  1546. error = mnt_want_write_file(filp);
  1547. if (error)
  1548. return error;
  1549. error = xfs_growfs_log(mp, &in);
  1550. mnt_drop_write_file(filp);
  1551. return error;
  1552. }
  1553. case XFS_IOC_FSGROWFSRT: {
  1554. xfs_growfs_rt_t in;
  1555. if (copy_from_user(&in, arg, sizeof(in)))
  1556. return -EFAULT;
  1557. error = mnt_want_write_file(filp);
  1558. if (error)
  1559. return error;
  1560. error = xfs_growfs_rt(mp, &in);
  1561. mnt_drop_write_file(filp);
  1562. return error;
  1563. }
  1564. case XFS_IOC_GOINGDOWN: {
  1565. __uint32_t in;
  1566. if (!capable(CAP_SYS_ADMIN))
  1567. return -EPERM;
  1568. if (get_user(in, (__uint32_t __user *)arg))
  1569. return -EFAULT;
  1570. return xfs_fs_goingdown(mp, in);
  1571. }
  1572. case XFS_IOC_ERROR_INJECTION: {
  1573. xfs_error_injection_t in;
  1574. if (!capable(CAP_SYS_ADMIN))
  1575. return -EPERM;
  1576. if (copy_from_user(&in, arg, sizeof(in)))
  1577. return -EFAULT;
  1578. return xfs_errortag_add(in.errtag, mp);
  1579. }
  1580. case XFS_IOC_ERROR_CLEARALL:
  1581. if (!capable(CAP_SYS_ADMIN))
  1582. return -EPERM;
  1583. return xfs_errortag_clearall(mp, 1);
  1584. case XFS_IOC_FREE_EOFBLOCKS: {
  1585. struct xfs_fs_eofblocks eofb;
  1586. struct xfs_eofblocks keofb;
  1587. if (!capable(CAP_SYS_ADMIN))
  1588. return -EPERM;
  1589. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1590. return -EROFS;
  1591. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1592. return -EFAULT;
  1593. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1594. if (error)
  1595. return error;
  1596. return xfs_icache_free_eofblocks(mp, &keofb);
  1597. }
  1598. default:
  1599. return -ENOTTY;
  1600. }
  1601. }