xfs_ioctl.c 35 KB

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