xfs_ioctl.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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 = -EPERM;
  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. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  335. xfs_fsop_attrlist_handlereq_t al_hreq;
  336. struct dentry *dentry;
  337. char *kbuf;
  338. if (!capable(CAP_SYS_ADMIN))
  339. return -EPERM;
  340. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  341. return -EFAULT;
  342. if (al_hreq.buflen < sizeof(struct attrlist) ||
  343. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  344. return -EINVAL;
  345. /*
  346. * Reject flags, only allow namespaces.
  347. */
  348. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  349. return -EINVAL;
  350. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  351. if (IS_ERR(dentry))
  352. return PTR_ERR(dentry);
  353. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  354. if (!kbuf)
  355. goto out_dput;
  356. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  357. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  358. al_hreq.flags, cursor);
  359. if (error)
  360. goto out_kfree;
  361. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  362. error = -EFAULT;
  363. goto out_kfree;
  364. }
  365. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  366. error = -EFAULT;
  367. out_kfree:
  368. kmem_free(kbuf);
  369. out_dput:
  370. dput(dentry);
  371. return error;
  372. }
  373. int
  374. xfs_attrmulti_attr_get(
  375. struct inode *inode,
  376. unsigned char *name,
  377. unsigned char __user *ubuf,
  378. __uint32_t *len,
  379. __uint32_t flags)
  380. {
  381. unsigned char *kbuf;
  382. int error = -EFAULT;
  383. if (*len > XFS_XATTR_SIZE_MAX)
  384. return -EINVAL;
  385. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  386. if (!kbuf)
  387. return -ENOMEM;
  388. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  389. if (error)
  390. goto out_kfree;
  391. if (copy_to_user(ubuf, kbuf, *len))
  392. error = -EFAULT;
  393. out_kfree:
  394. kmem_free(kbuf);
  395. return error;
  396. }
  397. int
  398. xfs_attrmulti_attr_set(
  399. struct inode *inode,
  400. unsigned char *name,
  401. const unsigned char __user *ubuf,
  402. __uint32_t len,
  403. __uint32_t flags)
  404. {
  405. unsigned char *kbuf;
  406. int error;
  407. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  408. return -EPERM;
  409. if (len > XFS_XATTR_SIZE_MAX)
  410. return -EINVAL;
  411. kbuf = memdup_user(ubuf, len);
  412. if (IS_ERR(kbuf))
  413. return PTR_ERR(kbuf);
  414. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  415. if (!error)
  416. xfs_forget_acl(inode, name, flags);
  417. kfree(kbuf);
  418. return error;
  419. }
  420. int
  421. xfs_attrmulti_attr_remove(
  422. struct inode *inode,
  423. unsigned char *name,
  424. __uint32_t flags)
  425. {
  426. int error;
  427. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  428. return -EPERM;
  429. error = xfs_attr_remove(XFS_I(inode), name, flags);
  430. if (!error)
  431. xfs_forget_acl(inode, name, flags);
  432. return error;
  433. }
  434. STATIC int
  435. xfs_attrmulti_by_handle(
  436. struct file *parfilp,
  437. void __user *arg)
  438. {
  439. int error;
  440. xfs_attr_multiop_t *ops;
  441. xfs_fsop_attrmulti_handlereq_t am_hreq;
  442. struct dentry *dentry;
  443. unsigned int i, size;
  444. unsigned char *attr_name;
  445. if (!capable(CAP_SYS_ADMIN))
  446. return -EPERM;
  447. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  448. return -EFAULT;
  449. /* overflow check */
  450. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  451. return -E2BIG;
  452. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  453. if (IS_ERR(dentry))
  454. return PTR_ERR(dentry);
  455. error = -E2BIG;
  456. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  457. if (!size || size > 16 * PAGE_SIZE)
  458. goto out_dput;
  459. ops = memdup_user(am_hreq.ops, size);
  460. if (IS_ERR(ops)) {
  461. error = PTR_ERR(ops);
  462. goto out_dput;
  463. }
  464. error = -ENOMEM;
  465. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  466. if (!attr_name)
  467. goto out_kfree_ops;
  468. error = 0;
  469. for (i = 0; i < am_hreq.opcount; i++) {
  470. ops[i].am_error = strncpy_from_user((char *)attr_name,
  471. ops[i].am_attrname, MAXNAMELEN);
  472. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  473. error = -ERANGE;
  474. if (ops[i].am_error < 0)
  475. break;
  476. switch (ops[i].am_opcode) {
  477. case ATTR_OP_GET:
  478. ops[i].am_error = xfs_attrmulti_attr_get(
  479. d_inode(dentry), attr_name,
  480. ops[i].am_attrvalue, &ops[i].am_length,
  481. ops[i].am_flags);
  482. break;
  483. case ATTR_OP_SET:
  484. ops[i].am_error = mnt_want_write_file(parfilp);
  485. if (ops[i].am_error)
  486. break;
  487. ops[i].am_error = xfs_attrmulti_attr_set(
  488. d_inode(dentry), attr_name,
  489. ops[i].am_attrvalue, ops[i].am_length,
  490. ops[i].am_flags);
  491. mnt_drop_write_file(parfilp);
  492. break;
  493. case ATTR_OP_REMOVE:
  494. ops[i].am_error = mnt_want_write_file(parfilp);
  495. if (ops[i].am_error)
  496. break;
  497. ops[i].am_error = xfs_attrmulti_attr_remove(
  498. d_inode(dentry), attr_name,
  499. ops[i].am_flags);
  500. mnt_drop_write_file(parfilp);
  501. break;
  502. default:
  503. ops[i].am_error = -EINVAL;
  504. }
  505. }
  506. if (copy_to_user(am_hreq.ops, ops, size))
  507. error = -EFAULT;
  508. kfree(attr_name);
  509. out_kfree_ops:
  510. kfree(ops);
  511. out_dput:
  512. dput(dentry);
  513. return error;
  514. }
  515. int
  516. xfs_ioc_space(
  517. struct file *filp,
  518. unsigned int cmd,
  519. xfs_flock64_t *bf)
  520. {
  521. struct inode *inode = file_inode(filp);
  522. struct xfs_inode *ip = XFS_I(inode);
  523. struct iattr iattr;
  524. enum xfs_prealloc_flags flags = 0;
  525. uint iolock = XFS_IOLOCK_EXCL;
  526. int error;
  527. /*
  528. * Only allow the sys admin to reserve space unless
  529. * unwritten extents are enabled.
  530. */
  531. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  532. !capable(CAP_SYS_ADMIN))
  533. return -EPERM;
  534. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  535. return -EPERM;
  536. if (!(filp->f_mode & FMODE_WRITE))
  537. return -EBADF;
  538. if (!S_ISREG(inode->i_mode))
  539. return -EINVAL;
  540. if (filp->f_flags & O_DSYNC)
  541. flags |= XFS_PREALLOC_SYNC;
  542. if (filp->f_mode & FMODE_NOCMTIME)
  543. flags |= XFS_PREALLOC_INVISIBLE;
  544. error = mnt_want_write_file(filp);
  545. if (error)
  546. return error;
  547. xfs_ilock(ip, iolock);
  548. error = xfs_break_layouts(inode, &iolock, false);
  549. if (error)
  550. goto out_unlock;
  551. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  552. iolock |= XFS_MMAPLOCK_EXCL;
  553. switch (bf->l_whence) {
  554. case 0: /*SEEK_SET*/
  555. break;
  556. case 1: /*SEEK_CUR*/
  557. bf->l_start += filp->f_pos;
  558. break;
  559. case 2: /*SEEK_END*/
  560. bf->l_start += XFS_ISIZE(ip);
  561. break;
  562. default:
  563. error = -EINVAL;
  564. goto out_unlock;
  565. }
  566. /*
  567. * length of <= 0 for resv/unresv/zero is invalid. length for
  568. * alloc/free is ignored completely and we have no idea what userspace
  569. * might have set it to, so set it to zero to allow range
  570. * checks to pass.
  571. */
  572. switch (cmd) {
  573. case XFS_IOC_ZERO_RANGE:
  574. case XFS_IOC_RESVSP:
  575. case XFS_IOC_RESVSP64:
  576. case XFS_IOC_UNRESVSP:
  577. case XFS_IOC_UNRESVSP64:
  578. if (bf->l_len <= 0) {
  579. error = -EINVAL;
  580. goto out_unlock;
  581. }
  582. break;
  583. default:
  584. bf->l_len = 0;
  585. break;
  586. }
  587. if (bf->l_start < 0 ||
  588. bf->l_start > inode->i_sb->s_maxbytes ||
  589. bf->l_start + bf->l_len < 0 ||
  590. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  591. error = -EINVAL;
  592. goto out_unlock;
  593. }
  594. switch (cmd) {
  595. case XFS_IOC_ZERO_RANGE:
  596. flags |= XFS_PREALLOC_SET;
  597. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  598. break;
  599. case XFS_IOC_RESVSP:
  600. case XFS_IOC_RESVSP64:
  601. flags |= XFS_PREALLOC_SET;
  602. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  603. XFS_BMAPI_PREALLOC);
  604. break;
  605. case XFS_IOC_UNRESVSP:
  606. case XFS_IOC_UNRESVSP64:
  607. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  608. break;
  609. case XFS_IOC_ALLOCSP:
  610. case XFS_IOC_ALLOCSP64:
  611. case XFS_IOC_FREESP:
  612. case XFS_IOC_FREESP64:
  613. flags |= XFS_PREALLOC_CLEAR;
  614. if (bf->l_start > XFS_ISIZE(ip)) {
  615. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  616. bf->l_start - XFS_ISIZE(ip), 0);
  617. if (error)
  618. goto out_unlock;
  619. }
  620. iattr.ia_valid = ATTR_SIZE;
  621. iattr.ia_size = bf->l_start;
  622. error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
  623. break;
  624. default:
  625. ASSERT(0);
  626. error = -EINVAL;
  627. }
  628. if (error)
  629. goto out_unlock;
  630. error = xfs_update_prealloc_flags(ip, flags);
  631. out_unlock:
  632. xfs_iunlock(ip, iolock);
  633. mnt_drop_write_file(filp);
  634. return error;
  635. }
  636. STATIC int
  637. xfs_ioc_bulkstat(
  638. xfs_mount_t *mp,
  639. unsigned int cmd,
  640. void __user *arg)
  641. {
  642. xfs_fsop_bulkreq_t bulkreq;
  643. int count; /* # of records returned */
  644. xfs_ino_t inlast; /* last inode number */
  645. int done;
  646. int error;
  647. /* done = 1 if there are more stats to get and if bulkstat */
  648. /* should be called again (unused here, but used in dmapi) */
  649. if (!capable(CAP_SYS_ADMIN))
  650. return -EPERM;
  651. if (XFS_FORCED_SHUTDOWN(mp))
  652. return -EIO;
  653. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  654. return -EFAULT;
  655. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  656. return -EFAULT;
  657. if ((count = bulkreq.icount) <= 0)
  658. return -EINVAL;
  659. if (bulkreq.ubuffer == NULL)
  660. return -EINVAL;
  661. if (cmd == XFS_IOC_FSINUMBERS)
  662. error = xfs_inumbers(mp, &inlast, &count,
  663. bulkreq.ubuffer, xfs_inumbers_fmt);
  664. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  665. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  666. sizeof(xfs_bstat_t), NULL, &done);
  667. else /* XFS_IOC_FSBULKSTAT */
  668. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  669. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  670. &done);
  671. if (error)
  672. return error;
  673. if (bulkreq.ocount != NULL) {
  674. if (copy_to_user(bulkreq.lastip, &inlast,
  675. sizeof(xfs_ino_t)))
  676. return -EFAULT;
  677. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  678. return -EFAULT;
  679. }
  680. return 0;
  681. }
  682. STATIC int
  683. xfs_ioc_fsgeometry_v1(
  684. xfs_mount_t *mp,
  685. void __user *arg)
  686. {
  687. xfs_fsop_geom_t fsgeo;
  688. int error;
  689. error = xfs_fs_geometry(mp, &fsgeo, 3);
  690. if (error)
  691. return error;
  692. /*
  693. * Caller should have passed an argument of type
  694. * xfs_fsop_geom_v1_t. This is a proper subset of the
  695. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  696. */
  697. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  698. return -EFAULT;
  699. return 0;
  700. }
  701. STATIC int
  702. xfs_ioc_fsgeometry(
  703. xfs_mount_t *mp,
  704. void __user *arg)
  705. {
  706. xfs_fsop_geom_t fsgeo;
  707. int error;
  708. error = xfs_fs_geometry(mp, &fsgeo, 4);
  709. if (error)
  710. return error;
  711. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  712. return -EFAULT;
  713. return 0;
  714. }
  715. /*
  716. * Linux extended inode flags interface.
  717. */
  718. STATIC unsigned int
  719. xfs_merge_ioc_xflags(
  720. unsigned int flags,
  721. unsigned int start)
  722. {
  723. unsigned int xflags = start;
  724. if (flags & FS_IMMUTABLE_FL)
  725. xflags |= FS_XFLAG_IMMUTABLE;
  726. else
  727. xflags &= ~FS_XFLAG_IMMUTABLE;
  728. if (flags & FS_APPEND_FL)
  729. xflags |= FS_XFLAG_APPEND;
  730. else
  731. xflags &= ~FS_XFLAG_APPEND;
  732. if (flags & FS_SYNC_FL)
  733. xflags |= FS_XFLAG_SYNC;
  734. else
  735. xflags &= ~FS_XFLAG_SYNC;
  736. if (flags & FS_NOATIME_FL)
  737. xflags |= FS_XFLAG_NOATIME;
  738. else
  739. xflags &= ~FS_XFLAG_NOATIME;
  740. if (flags & FS_NODUMP_FL)
  741. xflags |= FS_XFLAG_NODUMP;
  742. else
  743. xflags &= ~FS_XFLAG_NODUMP;
  744. return xflags;
  745. }
  746. STATIC unsigned int
  747. xfs_di2lxflags(
  748. __uint16_t di_flags)
  749. {
  750. unsigned int flags = 0;
  751. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  752. flags |= FS_IMMUTABLE_FL;
  753. if (di_flags & XFS_DIFLAG_APPEND)
  754. flags |= FS_APPEND_FL;
  755. if (di_flags & XFS_DIFLAG_SYNC)
  756. flags |= FS_SYNC_FL;
  757. if (di_flags & XFS_DIFLAG_NOATIME)
  758. flags |= FS_NOATIME_FL;
  759. if (di_flags & XFS_DIFLAG_NODUMP)
  760. flags |= FS_NODUMP_FL;
  761. return flags;
  762. }
  763. STATIC int
  764. xfs_ioc_fsgetxattr(
  765. xfs_inode_t *ip,
  766. int attr,
  767. void __user *arg)
  768. {
  769. struct fsxattr fa;
  770. memset(&fa, 0, sizeof(struct fsxattr));
  771. xfs_ilock(ip, XFS_ILOCK_SHARED);
  772. fa.fsx_xflags = xfs_ip2xflags(ip);
  773. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  774. fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
  775. ip->i_mount->m_sb.sb_blocklog;
  776. fa.fsx_projid = xfs_get_projid(ip);
  777. if (attr) {
  778. if (ip->i_afp) {
  779. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  780. fa.fsx_nextents = xfs_iext_count(ip->i_afp);
  781. else
  782. fa.fsx_nextents = ip->i_d.di_anextents;
  783. } else
  784. fa.fsx_nextents = 0;
  785. } else {
  786. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  787. fa.fsx_nextents = xfs_iext_count(&ip->i_df);
  788. else
  789. fa.fsx_nextents = ip->i_d.di_nextents;
  790. }
  791. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  792. if (copy_to_user(arg, &fa, sizeof(fa)))
  793. return -EFAULT;
  794. return 0;
  795. }
  796. STATIC uint16_t
  797. xfs_flags2diflags(
  798. struct xfs_inode *ip,
  799. unsigned int xflags)
  800. {
  801. /* can't set PREALLOC this way, just preserve it */
  802. uint16_t di_flags =
  803. (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  804. if (xflags & FS_XFLAG_IMMUTABLE)
  805. di_flags |= XFS_DIFLAG_IMMUTABLE;
  806. if (xflags & FS_XFLAG_APPEND)
  807. di_flags |= XFS_DIFLAG_APPEND;
  808. if (xflags & FS_XFLAG_SYNC)
  809. di_flags |= XFS_DIFLAG_SYNC;
  810. if (xflags & FS_XFLAG_NOATIME)
  811. di_flags |= XFS_DIFLAG_NOATIME;
  812. if (xflags & FS_XFLAG_NODUMP)
  813. di_flags |= XFS_DIFLAG_NODUMP;
  814. if (xflags & FS_XFLAG_NODEFRAG)
  815. di_flags |= XFS_DIFLAG_NODEFRAG;
  816. if (xflags & FS_XFLAG_FILESTREAM)
  817. di_flags |= XFS_DIFLAG_FILESTREAM;
  818. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  819. if (xflags & FS_XFLAG_RTINHERIT)
  820. di_flags |= XFS_DIFLAG_RTINHERIT;
  821. if (xflags & FS_XFLAG_NOSYMLINKS)
  822. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  823. if (xflags & FS_XFLAG_EXTSZINHERIT)
  824. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  825. if (xflags & FS_XFLAG_PROJINHERIT)
  826. di_flags |= XFS_DIFLAG_PROJINHERIT;
  827. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  828. if (xflags & FS_XFLAG_REALTIME)
  829. di_flags |= XFS_DIFLAG_REALTIME;
  830. if (xflags & FS_XFLAG_EXTSIZE)
  831. di_flags |= XFS_DIFLAG_EXTSIZE;
  832. }
  833. return di_flags;
  834. }
  835. STATIC uint64_t
  836. xfs_flags2diflags2(
  837. struct xfs_inode *ip,
  838. unsigned int xflags)
  839. {
  840. uint64_t di_flags2 =
  841. (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
  842. if (xflags & FS_XFLAG_DAX)
  843. di_flags2 |= XFS_DIFLAG2_DAX;
  844. if (xflags & FS_XFLAG_COWEXTSIZE)
  845. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  846. return di_flags2;
  847. }
  848. STATIC void
  849. xfs_diflags_to_linux(
  850. struct xfs_inode *ip)
  851. {
  852. struct inode *inode = VFS_I(ip);
  853. unsigned int xflags = xfs_ip2xflags(ip);
  854. if (xflags & FS_XFLAG_IMMUTABLE)
  855. inode->i_flags |= S_IMMUTABLE;
  856. else
  857. inode->i_flags &= ~S_IMMUTABLE;
  858. if (xflags & FS_XFLAG_APPEND)
  859. inode->i_flags |= S_APPEND;
  860. else
  861. inode->i_flags &= ~S_APPEND;
  862. if (xflags & FS_XFLAG_SYNC)
  863. inode->i_flags |= S_SYNC;
  864. else
  865. inode->i_flags &= ~S_SYNC;
  866. if (xflags & FS_XFLAG_NOATIME)
  867. inode->i_flags |= S_NOATIME;
  868. else
  869. inode->i_flags &= ~S_NOATIME;
  870. #if 0 /* disabled until the flag switching races are sorted out */
  871. if (xflags & FS_XFLAG_DAX)
  872. inode->i_flags |= S_DAX;
  873. else
  874. inode->i_flags &= ~S_DAX;
  875. #endif
  876. }
  877. static int
  878. xfs_ioctl_setattr_xflags(
  879. struct xfs_trans *tp,
  880. struct xfs_inode *ip,
  881. struct fsxattr *fa)
  882. {
  883. struct xfs_mount *mp = ip->i_mount;
  884. uint64_t di_flags2;
  885. /* Can't change realtime flag if any extents are allocated. */
  886. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  887. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  888. return -EINVAL;
  889. /* If realtime flag is set then must have realtime device */
  890. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  891. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  892. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  893. return -EINVAL;
  894. }
  895. /* Clear reflink if we are actually able to set the rt flag. */
  896. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  897. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  898. /* Don't allow us to set DAX mode for a reflinked file for now. */
  899. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  900. return -EINVAL;
  901. /*
  902. * Can't modify an immutable/append-only file unless
  903. * we have appropriate permission.
  904. */
  905. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  906. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  907. !capable(CAP_LINUX_IMMUTABLE))
  908. return -EPERM;
  909. /* diflags2 only valid for v3 inodes. */
  910. di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  911. if (di_flags2 && ip->i_d.di_version < 3)
  912. return -EINVAL;
  913. ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
  914. ip->i_d.di_flags2 = di_flags2;
  915. xfs_diflags_to_linux(ip);
  916. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  917. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  918. XFS_STATS_INC(mp, xs_ig_attrchg);
  919. return 0;
  920. }
  921. /*
  922. * If we are changing DAX flags, we have to ensure the file is clean and any
  923. * cached objects in the address space are invalidated and removed. This
  924. * requires us to lock out other IO and page faults similar to a truncate
  925. * operation. The locks need to be held until the transaction has been committed
  926. * so that the cache invalidation is atomic with respect to the DAX flag
  927. * manipulation.
  928. */
  929. static int
  930. xfs_ioctl_setattr_dax_invalidate(
  931. struct xfs_inode *ip,
  932. struct fsxattr *fa,
  933. int *join_flags)
  934. {
  935. struct inode *inode = VFS_I(ip);
  936. struct super_block *sb = inode->i_sb;
  937. int error;
  938. *join_flags = 0;
  939. /*
  940. * It is only valid to set the DAX flag on regular files and
  941. * directories on filesystems where the block size is equal to the page
  942. * size. On directories it serves as an inherit hint.
  943. */
  944. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  945. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  946. return -EINVAL;
  947. if (bdev_dax_supported(sb, sb->s_blocksize) < 0)
  948. return -EINVAL;
  949. }
  950. /* If the DAX state is not changing, we have nothing to do here. */
  951. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  952. return 0;
  953. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  954. return 0;
  955. /* lock, flush and invalidate mapping in preparation for flag change */
  956. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  957. error = filemap_write_and_wait(inode->i_mapping);
  958. if (error)
  959. goto out_unlock;
  960. error = invalidate_inode_pages2(inode->i_mapping);
  961. if (error)
  962. goto out_unlock;
  963. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  964. return 0;
  965. out_unlock:
  966. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  967. return error;
  968. }
  969. /*
  970. * Set up the transaction structure for the setattr operation, checking that we
  971. * have permission to do so. On success, return a clean transaction and the
  972. * inode locked exclusively ready for further operation specific checks. On
  973. * failure, return an error without modifying or locking the inode.
  974. *
  975. * The inode might already be IO locked on call. If this is the case, it is
  976. * indicated in @join_flags and we take full responsibility for ensuring they
  977. * are unlocked from now on. Hence if we have an error here, we still have to
  978. * unlock them. Otherwise, once they are joined to the transaction, they will
  979. * be unlocked on commit/cancel.
  980. */
  981. static struct xfs_trans *
  982. xfs_ioctl_setattr_get_trans(
  983. struct xfs_inode *ip,
  984. int join_flags)
  985. {
  986. struct xfs_mount *mp = ip->i_mount;
  987. struct xfs_trans *tp;
  988. int error = -EROFS;
  989. if (mp->m_flags & XFS_MOUNT_RDONLY)
  990. goto out_unlock;
  991. error = -EIO;
  992. if (XFS_FORCED_SHUTDOWN(mp))
  993. goto out_unlock;
  994. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  995. if (error)
  996. return ERR_PTR(error);
  997. xfs_ilock(ip, XFS_ILOCK_EXCL);
  998. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  999. join_flags = 0;
  1000. /*
  1001. * CAP_FOWNER overrides the following restrictions:
  1002. *
  1003. * The user ID of the calling process must be equal to the file owner
  1004. * ID, except in cases where the CAP_FSETID capability is applicable.
  1005. */
  1006. if (!inode_owner_or_capable(VFS_I(ip))) {
  1007. error = -EPERM;
  1008. goto out_cancel;
  1009. }
  1010. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1011. xfs_trans_set_sync(tp);
  1012. return tp;
  1013. out_cancel:
  1014. xfs_trans_cancel(tp);
  1015. out_unlock:
  1016. if (join_flags)
  1017. xfs_iunlock(ip, join_flags);
  1018. return ERR_PTR(error);
  1019. }
  1020. /*
  1021. * extent size hint validation is somewhat cumbersome. Rules are:
  1022. *
  1023. * 1. extent size hint is only valid for directories and regular files
  1024. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1025. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1026. * 4. can only be changed on regular files if no extents are allocated
  1027. * 5. can be changed on directories at any time
  1028. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1029. * 7. Extent size must be a multiple of the appropriate block size.
  1030. * 8. for non-realtime files, the extent size hint must be limited
  1031. * to half the AG size to avoid alignment extending the extent beyond the
  1032. * limits of the AG.
  1033. */
  1034. static int
  1035. xfs_ioctl_setattr_check_extsize(
  1036. struct xfs_inode *ip,
  1037. struct fsxattr *fa)
  1038. {
  1039. struct xfs_mount *mp = ip->i_mount;
  1040. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1041. return -EINVAL;
  1042. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1043. !S_ISDIR(VFS_I(ip)->i_mode))
  1044. return -EINVAL;
  1045. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1046. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1047. return -EINVAL;
  1048. if (fa->fsx_extsize != 0) {
  1049. xfs_extlen_t size;
  1050. xfs_fsblock_t extsize_fsb;
  1051. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1052. if (extsize_fsb > MAXEXTLEN)
  1053. return -EINVAL;
  1054. if (XFS_IS_REALTIME_INODE(ip) ||
  1055. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1056. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1057. } else {
  1058. size = mp->m_sb.sb_blocksize;
  1059. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1060. return -EINVAL;
  1061. }
  1062. if (fa->fsx_extsize % size)
  1063. return -EINVAL;
  1064. } else
  1065. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1066. return 0;
  1067. }
  1068. /*
  1069. * CoW extent size hint validation rules are:
  1070. *
  1071. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  1072. * The inode does not have to have any shared blocks, but it must be a v3.
  1073. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  1074. * for a directory, the hint is propagated to new files.
  1075. * 3. Can be changed on files & directories at any time.
  1076. * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
  1077. * 5. Extent size must be a multiple of the appropriate block size.
  1078. * 6. The extent size hint must be limited to half the AG size to avoid
  1079. * alignment extending the extent beyond the limits of the AG.
  1080. */
  1081. static int
  1082. xfs_ioctl_setattr_check_cowextsize(
  1083. struct xfs_inode *ip,
  1084. struct fsxattr *fa)
  1085. {
  1086. struct xfs_mount *mp = ip->i_mount;
  1087. if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
  1088. return 0;
  1089. if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
  1090. ip->i_d.di_version != 3)
  1091. return -EINVAL;
  1092. if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
  1093. return -EINVAL;
  1094. if (fa->fsx_cowextsize != 0) {
  1095. xfs_extlen_t size;
  1096. xfs_fsblock_t cowextsize_fsb;
  1097. cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1098. if (cowextsize_fsb > MAXEXTLEN)
  1099. return -EINVAL;
  1100. size = mp->m_sb.sb_blocksize;
  1101. if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
  1102. return -EINVAL;
  1103. if (fa->fsx_cowextsize % size)
  1104. return -EINVAL;
  1105. } else
  1106. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  1107. return 0;
  1108. }
  1109. static int
  1110. xfs_ioctl_setattr_check_projid(
  1111. struct xfs_inode *ip,
  1112. struct fsxattr *fa)
  1113. {
  1114. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1115. if (fa->fsx_projid > (__uint16_t)-1 &&
  1116. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1117. return -EINVAL;
  1118. /*
  1119. * Project Quota ID state is only allowed to change from within the init
  1120. * namespace. Enforce that restriction only if we are trying to change
  1121. * the quota ID state. Everything else is allowed in user namespaces.
  1122. */
  1123. if (current_user_ns() == &init_user_ns)
  1124. return 0;
  1125. if (xfs_get_projid(ip) != fa->fsx_projid)
  1126. return -EINVAL;
  1127. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1128. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1129. return -EINVAL;
  1130. return 0;
  1131. }
  1132. STATIC int
  1133. xfs_ioctl_setattr(
  1134. xfs_inode_t *ip,
  1135. struct fsxattr *fa)
  1136. {
  1137. struct xfs_mount *mp = ip->i_mount;
  1138. struct xfs_trans *tp;
  1139. struct xfs_dquot *udqp = NULL;
  1140. struct xfs_dquot *pdqp = NULL;
  1141. struct xfs_dquot *olddquot = NULL;
  1142. int code;
  1143. int join_flags = 0;
  1144. trace_xfs_ioctl_setattr(ip);
  1145. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1146. if (code)
  1147. return code;
  1148. /*
  1149. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1150. * before we start any other transactions. Trying to do this later
  1151. * is messy. We don't care to take a readlock to look at the ids
  1152. * in inode here, because we can't hold it across the trans_reserve.
  1153. * If the IDs do change before we take the ilock, we're covered
  1154. * because the i_*dquot fields will get updated anyway.
  1155. */
  1156. if (XFS_IS_QUOTA_ON(mp)) {
  1157. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1158. ip->i_d.di_gid, fa->fsx_projid,
  1159. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1160. if (code)
  1161. return code;
  1162. }
  1163. /*
  1164. * Changing DAX config may require inode locking for mapping
  1165. * invalidation. These need to be held all the way to transaction commit
  1166. * or cancel time, so need to be passed through to
  1167. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1168. * appropriately.
  1169. */
  1170. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1171. if (code)
  1172. goto error_free_dquots;
  1173. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1174. if (IS_ERR(tp)) {
  1175. code = PTR_ERR(tp);
  1176. goto error_free_dquots;
  1177. }
  1178. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1179. xfs_get_projid(ip) != fa->fsx_projid) {
  1180. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1181. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1182. if (code) /* out of quota */
  1183. goto error_trans_cancel;
  1184. }
  1185. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1186. if (code)
  1187. goto error_trans_cancel;
  1188. code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1189. if (code)
  1190. goto error_trans_cancel;
  1191. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1192. if (code)
  1193. goto error_trans_cancel;
  1194. /*
  1195. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1196. * overrides the following restrictions:
  1197. *
  1198. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1199. * successful return from chown()
  1200. */
  1201. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1202. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1203. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1204. /* Change the ownerships and register project quota modifications */
  1205. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1206. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1207. olddquot = xfs_qm_vop_chown(tp, ip,
  1208. &ip->i_pdquot, pdqp);
  1209. }
  1210. ASSERT(ip->i_d.di_version > 1);
  1211. xfs_set_projid(ip, fa->fsx_projid);
  1212. }
  1213. /*
  1214. * Only set the extent size hint if we've already determined that the
  1215. * extent size hint should be set on the inode. If no extent size flags
  1216. * are set on the inode then unconditionally clear the extent size hint.
  1217. */
  1218. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1219. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1220. else
  1221. ip->i_d.di_extsize = 0;
  1222. if (ip->i_d.di_version == 3 &&
  1223. (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1224. ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
  1225. mp->m_sb.sb_blocklog;
  1226. else
  1227. ip->i_d.di_cowextsize = 0;
  1228. code = xfs_trans_commit(tp);
  1229. /*
  1230. * Release any dquot(s) the inode had kept before chown.
  1231. */
  1232. xfs_qm_dqrele(olddquot);
  1233. xfs_qm_dqrele(udqp);
  1234. xfs_qm_dqrele(pdqp);
  1235. return code;
  1236. error_trans_cancel:
  1237. xfs_trans_cancel(tp);
  1238. error_free_dquots:
  1239. xfs_qm_dqrele(udqp);
  1240. xfs_qm_dqrele(pdqp);
  1241. return code;
  1242. }
  1243. STATIC int
  1244. xfs_ioc_fssetxattr(
  1245. xfs_inode_t *ip,
  1246. struct file *filp,
  1247. void __user *arg)
  1248. {
  1249. struct fsxattr fa;
  1250. int error;
  1251. if (copy_from_user(&fa, arg, sizeof(fa)))
  1252. return -EFAULT;
  1253. error = mnt_want_write_file(filp);
  1254. if (error)
  1255. return error;
  1256. error = xfs_ioctl_setattr(ip, &fa);
  1257. mnt_drop_write_file(filp);
  1258. return error;
  1259. }
  1260. STATIC int
  1261. xfs_ioc_getxflags(
  1262. xfs_inode_t *ip,
  1263. void __user *arg)
  1264. {
  1265. unsigned int flags;
  1266. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1267. if (copy_to_user(arg, &flags, sizeof(flags)))
  1268. return -EFAULT;
  1269. return 0;
  1270. }
  1271. STATIC int
  1272. xfs_ioc_setxflags(
  1273. struct xfs_inode *ip,
  1274. struct file *filp,
  1275. void __user *arg)
  1276. {
  1277. struct xfs_trans *tp;
  1278. struct fsxattr fa;
  1279. unsigned int flags;
  1280. int join_flags = 0;
  1281. int error;
  1282. if (copy_from_user(&flags, arg, sizeof(flags)))
  1283. return -EFAULT;
  1284. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1285. FS_NOATIME_FL | FS_NODUMP_FL | \
  1286. FS_SYNC_FL))
  1287. return -EOPNOTSUPP;
  1288. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1289. error = mnt_want_write_file(filp);
  1290. if (error)
  1291. return error;
  1292. /*
  1293. * Changing DAX config may require inode locking for mapping
  1294. * invalidation. These need to be held all the way to transaction commit
  1295. * or cancel time, so need to be passed through to
  1296. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1297. * appropriately.
  1298. */
  1299. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1300. if (error)
  1301. goto out_drop_write;
  1302. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1303. if (IS_ERR(tp)) {
  1304. error = PTR_ERR(tp);
  1305. goto out_drop_write;
  1306. }
  1307. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1308. if (error) {
  1309. xfs_trans_cancel(tp);
  1310. goto out_drop_write;
  1311. }
  1312. error = xfs_trans_commit(tp);
  1313. out_drop_write:
  1314. mnt_drop_write_file(filp);
  1315. return error;
  1316. }
  1317. STATIC int
  1318. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1319. {
  1320. struct getbmap __user *base = (struct getbmap __user *)*ap;
  1321. /* copy only getbmap portion (not getbmapx) */
  1322. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1323. return -EFAULT;
  1324. *ap += sizeof(struct getbmap);
  1325. return 0;
  1326. }
  1327. STATIC int
  1328. xfs_ioc_getbmap(
  1329. struct file *file,
  1330. unsigned int cmd,
  1331. void __user *arg)
  1332. {
  1333. struct getbmapx bmx = { 0 };
  1334. int error;
  1335. /* struct getbmap is a strict subset of struct getbmapx. */
  1336. if (copy_from_user(&bmx, arg, offsetof(struct getbmapx, bmv_iflags)))
  1337. return -EFAULT;
  1338. if (bmx.bmv_count < 2)
  1339. return -EINVAL;
  1340. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1341. if (file->f_mode & FMODE_NOCMTIME)
  1342. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1343. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, xfs_getbmap_format,
  1344. (__force struct getbmap *)arg+1);
  1345. if (error)
  1346. return error;
  1347. /* copy back header - only size of getbmap */
  1348. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1349. return -EFAULT;
  1350. return 0;
  1351. }
  1352. STATIC int
  1353. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1354. {
  1355. struct getbmapx __user *base = (struct getbmapx __user *)*ap;
  1356. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1357. return -EFAULT;
  1358. *ap += sizeof(struct getbmapx);
  1359. return 0;
  1360. }
  1361. STATIC int
  1362. xfs_ioc_getbmapx(
  1363. struct xfs_inode *ip,
  1364. void __user *arg)
  1365. {
  1366. struct getbmapx bmx;
  1367. int error;
  1368. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1369. return -EFAULT;
  1370. if (bmx.bmv_count < 2)
  1371. return -EINVAL;
  1372. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1373. return -EINVAL;
  1374. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1375. (__force struct getbmapx *)arg+1);
  1376. if (error)
  1377. return error;
  1378. /* copy back header */
  1379. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1380. return -EFAULT;
  1381. return 0;
  1382. }
  1383. int
  1384. xfs_ioc_swapext(
  1385. xfs_swapext_t *sxp)
  1386. {
  1387. xfs_inode_t *ip, *tip;
  1388. struct fd f, tmp;
  1389. int error = 0;
  1390. /* Pull information for the target fd */
  1391. f = fdget((int)sxp->sx_fdtarget);
  1392. if (!f.file) {
  1393. error = -EINVAL;
  1394. goto out;
  1395. }
  1396. if (!(f.file->f_mode & FMODE_WRITE) ||
  1397. !(f.file->f_mode & FMODE_READ) ||
  1398. (f.file->f_flags & O_APPEND)) {
  1399. error = -EBADF;
  1400. goto out_put_file;
  1401. }
  1402. tmp = fdget((int)sxp->sx_fdtmp);
  1403. if (!tmp.file) {
  1404. error = -EINVAL;
  1405. goto out_put_file;
  1406. }
  1407. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1408. !(tmp.file->f_mode & FMODE_READ) ||
  1409. (tmp.file->f_flags & O_APPEND)) {
  1410. error = -EBADF;
  1411. goto out_put_tmp_file;
  1412. }
  1413. if (IS_SWAPFILE(file_inode(f.file)) ||
  1414. IS_SWAPFILE(file_inode(tmp.file))) {
  1415. error = -EINVAL;
  1416. goto out_put_tmp_file;
  1417. }
  1418. /*
  1419. * We need to ensure that the fds passed in point to XFS inodes
  1420. * before we cast and access them as XFS structures as we have no
  1421. * control over what the user passes us here.
  1422. */
  1423. if (f.file->f_op != &xfs_file_operations ||
  1424. tmp.file->f_op != &xfs_file_operations) {
  1425. error = -EINVAL;
  1426. goto out_put_tmp_file;
  1427. }
  1428. ip = XFS_I(file_inode(f.file));
  1429. tip = XFS_I(file_inode(tmp.file));
  1430. if (ip->i_mount != tip->i_mount) {
  1431. error = -EINVAL;
  1432. goto out_put_tmp_file;
  1433. }
  1434. if (ip->i_ino == tip->i_ino) {
  1435. error = -EINVAL;
  1436. goto out_put_tmp_file;
  1437. }
  1438. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1439. error = -EIO;
  1440. goto out_put_tmp_file;
  1441. }
  1442. error = xfs_swap_extents(ip, tip, sxp);
  1443. out_put_tmp_file:
  1444. fdput(tmp);
  1445. out_put_file:
  1446. fdput(f);
  1447. out:
  1448. return error;
  1449. }
  1450. /*
  1451. * Note: some of the ioctl's return positive numbers as a
  1452. * byte count indicating success, such as readlink_by_handle.
  1453. * So we don't "sign flip" like most other routines. This means
  1454. * true errors need to be returned as a negative value.
  1455. */
  1456. long
  1457. xfs_file_ioctl(
  1458. struct file *filp,
  1459. unsigned int cmd,
  1460. unsigned long p)
  1461. {
  1462. struct inode *inode = file_inode(filp);
  1463. struct xfs_inode *ip = XFS_I(inode);
  1464. struct xfs_mount *mp = ip->i_mount;
  1465. void __user *arg = (void __user *)p;
  1466. int error;
  1467. trace_xfs_file_ioctl(ip);
  1468. switch (cmd) {
  1469. case FITRIM:
  1470. return xfs_ioc_trim(mp, arg);
  1471. case XFS_IOC_ALLOCSP:
  1472. case XFS_IOC_FREESP:
  1473. case XFS_IOC_RESVSP:
  1474. case XFS_IOC_UNRESVSP:
  1475. case XFS_IOC_ALLOCSP64:
  1476. case XFS_IOC_FREESP64:
  1477. case XFS_IOC_RESVSP64:
  1478. case XFS_IOC_UNRESVSP64:
  1479. case XFS_IOC_ZERO_RANGE: {
  1480. xfs_flock64_t bf;
  1481. if (copy_from_user(&bf, arg, sizeof(bf)))
  1482. return -EFAULT;
  1483. return xfs_ioc_space(filp, cmd, &bf);
  1484. }
  1485. case XFS_IOC_DIOINFO: {
  1486. struct dioattr da;
  1487. xfs_buftarg_t *target =
  1488. XFS_IS_REALTIME_INODE(ip) ?
  1489. mp->m_rtdev_targp : mp->m_ddev_targp;
  1490. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1491. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1492. if (copy_to_user(arg, &da, sizeof(da)))
  1493. return -EFAULT;
  1494. return 0;
  1495. }
  1496. case XFS_IOC_FSBULKSTAT_SINGLE:
  1497. case XFS_IOC_FSBULKSTAT:
  1498. case XFS_IOC_FSINUMBERS:
  1499. return xfs_ioc_bulkstat(mp, cmd, arg);
  1500. case XFS_IOC_FSGEOMETRY_V1:
  1501. return xfs_ioc_fsgeometry_v1(mp, arg);
  1502. case XFS_IOC_FSGEOMETRY:
  1503. return xfs_ioc_fsgeometry(mp, arg);
  1504. case XFS_IOC_GETVERSION:
  1505. return put_user(inode->i_generation, (int __user *)arg);
  1506. case XFS_IOC_FSGETXATTR:
  1507. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1508. case XFS_IOC_FSGETXATTRA:
  1509. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1510. case XFS_IOC_FSSETXATTR:
  1511. return xfs_ioc_fssetxattr(ip, filp, arg);
  1512. case XFS_IOC_GETXFLAGS:
  1513. return xfs_ioc_getxflags(ip, arg);
  1514. case XFS_IOC_SETXFLAGS:
  1515. return xfs_ioc_setxflags(ip, filp, arg);
  1516. case XFS_IOC_FSSETDM: {
  1517. struct fsdmidata dmi;
  1518. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1519. return -EFAULT;
  1520. error = mnt_want_write_file(filp);
  1521. if (error)
  1522. return error;
  1523. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1524. dmi.fsd_dmstate);
  1525. mnt_drop_write_file(filp);
  1526. return error;
  1527. }
  1528. case XFS_IOC_GETBMAP:
  1529. case XFS_IOC_GETBMAPA:
  1530. return xfs_ioc_getbmap(filp, cmd, arg);
  1531. case XFS_IOC_GETBMAPX:
  1532. return xfs_ioc_getbmapx(ip, arg);
  1533. case XFS_IOC_FD_TO_HANDLE:
  1534. case XFS_IOC_PATH_TO_HANDLE:
  1535. case XFS_IOC_PATH_TO_FSHANDLE: {
  1536. xfs_fsop_handlereq_t hreq;
  1537. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1538. return -EFAULT;
  1539. return xfs_find_handle(cmd, &hreq);
  1540. }
  1541. case XFS_IOC_OPEN_BY_HANDLE: {
  1542. xfs_fsop_handlereq_t hreq;
  1543. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1544. return -EFAULT;
  1545. return xfs_open_by_handle(filp, &hreq);
  1546. }
  1547. case XFS_IOC_FSSETDM_BY_HANDLE:
  1548. return xfs_fssetdm_by_handle(filp, arg);
  1549. case XFS_IOC_READLINK_BY_HANDLE: {
  1550. xfs_fsop_handlereq_t hreq;
  1551. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1552. return -EFAULT;
  1553. return xfs_readlink_by_handle(filp, &hreq);
  1554. }
  1555. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1556. return xfs_attrlist_by_handle(filp, arg);
  1557. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1558. return xfs_attrmulti_by_handle(filp, arg);
  1559. case XFS_IOC_SWAPEXT: {
  1560. struct xfs_swapext sxp;
  1561. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1562. return -EFAULT;
  1563. error = mnt_want_write_file(filp);
  1564. if (error)
  1565. return error;
  1566. error = xfs_ioc_swapext(&sxp);
  1567. mnt_drop_write_file(filp);
  1568. return error;
  1569. }
  1570. case XFS_IOC_FSCOUNTS: {
  1571. xfs_fsop_counts_t out;
  1572. error = xfs_fs_counts(mp, &out);
  1573. if (error)
  1574. return error;
  1575. if (copy_to_user(arg, &out, sizeof(out)))
  1576. return -EFAULT;
  1577. return 0;
  1578. }
  1579. case XFS_IOC_SET_RESBLKS: {
  1580. xfs_fsop_resblks_t inout;
  1581. __uint64_t in;
  1582. if (!capable(CAP_SYS_ADMIN))
  1583. return -EPERM;
  1584. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1585. return -EROFS;
  1586. if (copy_from_user(&inout, arg, sizeof(inout)))
  1587. return -EFAULT;
  1588. error = mnt_want_write_file(filp);
  1589. if (error)
  1590. return error;
  1591. /* input parameter is passed in resblks field of structure */
  1592. in = inout.resblks;
  1593. error = xfs_reserve_blocks(mp, &in, &inout);
  1594. mnt_drop_write_file(filp);
  1595. if (error)
  1596. return error;
  1597. if (copy_to_user(arg, &inout, sizeof(inout)))
  1598. return -EFAULT;
  1599. return 0;
  1600. }
  1601. case XFS_IOC_GET_RESBLKS: {
  1602. xfs_fsop_resblks_t out;
  1603. if (!capable(CAP_SYS_ADMIN))
  1604. return -EPERM;
  1605. error = xfs_reserve_blocks(mp, NULL, &out);
  1606. if (error)
  1607. return error;
  1608. if (copy_to_user(arg, &out, sizeof(out)))
  1609. return -EFAULT;
  1610. return 0;
  1611. }
  1612. case XFS_IOC_FSGROWFSDATA: {
  1613. xfs_growfs_data_t in;
  1614. if (copy_from_user(&in, arg, sizeof(in)))
  1615. return -EFAULT;
  1616. error = mnt_want_write_file(filp);
  1617. if (error)
  1618. return error;
  1619. error = xfs_growfs_data(mp, &in);
  1620. mnt_drop_write_file(filp);
  1621. return error;
  1622. }
  1623. case XFS_IOC_FSGROWFSLOG: {
  1624. xfs_growfs_log_t in;
  1625. if (copy_from_user(&in, arg, sizeof(in)))
  1626. return -EFAULT;
  1627. error = mnt_want_write_file(filp);
  1628. if (error)
  1629. return error;
  1630. error = xfs_growfs_log(mp, &in);
  1631. mnt_drop_write_file(filp);
  1632. return error;
  1633. }
  1634. case XFS_IOC_FSGROWFSRT: {
  1635. xfs_growfs_rt_t in;
  1636. if (copy_from_user(&in, arg, sizeof(in)))
  1637. return -EFAULT;
  1638. error = mnt_want_write_file(filp);
  1639. if (error)
  1640. return error;
  1641. error = xfs_growfs_rt(mp, &in);
  1642. mnt_drop_write_file(filp);
  1643. return error;
  1644. }
  1645. case XFS_IOC_GOINGDOWN: {
  1646. __uint32_t in;
  1647. if (!capable(CAP_SYS_ADMIN))
  1648. return -EPERM;
  1649. if (get_user(in, (__uint32_t __user *)arg))
  1650. return -EFAULT;
  1651. return xfs_fs_goingdown(mp, in);
  1652. }
  1653. case XFS_IOC_ERROR_INJECTION: {
  1654. xfs_error_injection_t in;
  1655. if (!capable(CAP_SYS_ADMIN))
  1656. return -EPERM;
  1657. if (copy_from_user(&in, arg, sizeof(in)))
  1658. return -EFAULT;
  1659. return xfs_errortag_add(in.errtag, mp);
  1660. }
  1661. case XFS_IOC_ERROR_CLEARALL:
  1662. if (!capable(CAP_SYS_ADMIN))
  1663. return -EPERM;
  1664. return xfs_errortag_clearall(mp, 1);
  1665. case XFS_IOC_FREE_EOFBLOCKS: {
  1666. struct xfs_fs_eofblocks eofb;
  1667. struct xfs_eofblocks keofb;
  1668. if (!capable(CAP_SYS_ADMIN))
  1669. return -EPERM;
  1670. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1671. return -EROFS;
  1672. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1673. return -EFAULT;
  1674. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1675. if (error)
  1676. return error;
  1677. return xfs_icache_free_eofblocks(mp, &keofb);
  1678. }
  1679. default:
  1680. return -ENOTTY;
  1681. }
  1682. }