xfs_ioctl.c 35 KB

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