xfs_ioctl.c 35 KB

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