xfs_ioctl.c 41 KB

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