ioctl.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/time.h>
  27. #include <linux/init.h>
  28. #include <linux/string.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/swap.h>
  34. #include <linux/writeback.h>
  35. #include <linux/statfs.h>
  36. #include <linux/compat.h>
  37. #include <linux/bit_spinlock.h>
  38. #include <linux/security.h>
  39. #include <linux/xattr.h>
  40. #include <linux/vmalloc.h>
  41. #include "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "volumes.h"
  49. #include "locking.h"
  50. #include "ctree.h"
  51. /* Mask out flags that are inappropriate for the given type of inode. */
  52. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  53. {
  54. if (S_ISDIR(mode))
  55. return flags;
  56. else if (S_ISREG(mode))
  57. return flags & ~FS_DIRSYNC_FL;
  58. else
  59. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  60. }
  61. /*
  62. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  63. */
  64. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  65. {
  66. unsigned int iflags = 0;
  67. if (flags & BTRFS_INODE_SYNC)
  68. iflags |= FS_SYNC_FL;
  69. if (flags & BTRFS_INODE_IMMUTABLE)
  70. iflags |= FS_IMMUTABLE_FL;
  71. if (flags & BTRFS_INODE_APPEND)
  72. iflags |= FS_APPEND_FL;
  73. if (flags & BTRFS_INODE_NODUMP)
  74. iflags |= FS_NODUMP_FL;
  75. if (flags & BTRFS_INODE_NOATIME)
  76. iflags |= FS_NOATIME_FL;
  77. if (flags & BTRFS_INODE_DIRSYNC)
  78. iflags |= FS_DIRSYNC_FL;
  79. return iflags;
  80. }
  81. /*
  82. * Update inode->i_flags based on the btrfs internal flags.
  83. */
  84. void btrfs_update_iflags(struct inode *inode)
  85. {
  86. struct btrfs_inode *ip = BTRFS_I(inode);
  87. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  88. if (ip->flags & BTRFS_INODE_SYNC)
  89. inode->i_flags |= S_SYNC;
  90. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  91. inode->i_flags |= S_IMMUTABLE;
  92. if (ip->flags & BTRFS_INODE_APPEND)
  93. inode->i_flags |= S_APPEND;
  94. if (ip->flags & BTRFS_INODE_NOATIME)
  95. inode->i_flags |= S_NOATIME;
  96. if (ip->flags & BTRFS_INODE_DIRSYNC)
  97. inode->i_flags |= S_DIRSYNC;
  98. }
  99. /*
  100. * Inherit flags from the parent inode.
  101. *
  102. * Unlike extN we don't have any flags we don't want to inherit currently.
  103. */
  104. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  105. {
  106. unsigned int flags;
  107. if (!dir)
  108. return;
  109. flags = BTRFS_I(dir)->flags;
  110. if (S_ISREG(inode->i_mode))
  111. flags &= ~BTRFS_INODE_DIRSYNC;
  112. else if (!S_ISDIR(inode->i_mode))
  113. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  114. BTRFS_I(inode)->flags = flags;
  115. btrfs_update_iflags(inode);
  116. }
  117. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  118. {
  119. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  120. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  121. if (copy_to_user(arg, &flags, sizeof(flags)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  126. {
  127. struct inode *inode = file->f_path.dentry->d_inode;
  128. struct btrfs_inode *ip = BTRFS_I(inode);
  129. struct btrfs_root *root = ip->root;
  130. struct btrfs_trans_handle *trans;
  131. unsigned int flags, oldflags;
  132. int ret;
  133. if (copy_from_user(&flags, arg, sizeof(flags)))
  134. return -EFAULT;
  135. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  136. FS_NOATIME_FL | FS_NODUMP_FL | \
  137. FS_SYNC_FL | FS_DIRSYNC_FL))
  138. return -EOPNOTSUPP;
  139. if (!is_owner_or_cap(inode))
  140. return -EACCES;
  141. mutex_lock(&inode->i_mutex);
  142. flags = btrfs_mask_flags(inode->i_mode, flags);
  143. oldflags = btrfs_flags_to_ioctl(ip->flags);
  144. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  145. if (!capable(CAP_LINUX_IMMUTABLE)) {
  146. ret = -EPERM;
  147. goto out_unlock;
  148. }
  149. }
  150. ret = mnt_want_write(file->f_path.mnt);
  151. if (ret)
  152. goto out_unlock;
  153. if (flags & FS_SYNC_FL)
  154. ip->flags |= BTRFS_INODE_SYNC;
  155. else
  156. ip->flags &= ~BTRFS_INODE_SYNC;
  157. if (flags & FS_IMMUTABLE_FL)
  158. ip->flags |= BTRFS_INODE_IMMUTABLE;
  159. else
  160. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  161. if (flags & FS_APPEND_FL)
  162. ip->flags |= BTRFS_INODE_APPEND;
  163. else
  164. ip->flags &= ~BTRFS_INODE_APPEND;
  165. if (flags & FS_NODUMP_FL)
  166. ip->flags |= BTRFS_INODE_NODUMP;
  167. else
  168. ip->flags &= ~BTRFS_INODE_NODUMP;
  169. if (flags & FS_NOATIME_FL)
  170. ip->flags |= BTRFS_INODE_NOATIME;
  171. else
  172. ip->flags &= ~BTRFS_INODE_NOATIME;
  173. if (flags & FS_DIRSYNC_FL)
  174. ip->flags |= BTRFS_INODE_DIRSYNC;
  175. else
  176. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  177. trans = btrfs_join_transaction(root, 1);
  178. BUG_ON(!trans);
  179. ret = btrfs_update_inode(trans, root, inode);
  180. BUG_ON(ret);
  181. btrfs_update_iflags(inode);
  182. inode->i_ctime = CURRENT_TIME;
  183. btrfs_end_transaction(trans, root);
  184. mnt_drop_write(file->f_path.mnt);
  185. out_unlock:
  186. mutex_unlock(&inode->i_mutex);
  187. return 0;
  188. }
  189. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  190. {
  191. struct inode *inode = file->f_path.dentry->d_inode;
  192. return put_user(inode->i_generation, arg);
  193. }
  194. static noinline int create_subvol(struct btrfs_root *root,
  195. struct dentry *dentry,
  196. char *name, int namelen)
  197. {
  198. struct btrfs_trans_handle *trans;
  199. struct btrfs_key key;
  200. struct btrfs_root_item root_item;
  201. struct btrfs_inode_item *inode_item;
  202. struct extent_buffer *leaf;
  203. struct btrfs_root *new_root;
  204. struct inode *dir = dentry->d_parent->d_inode;
  205. int ret;
  206. int err;
  207. u64 objectid;
  208. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  209. u64 index = 0;
  210. /*
  211. * 1 - inode item
  212. * 2 - refs
  213. * 1 - root item
  214. * 2 - dir items
  215. */
  216. ret = btrfs_reserve_metadata_space(root, 6);
  217. if (ret)
  218. return ret;
  219. trans = btrfs_start_transaction(root, 1);
  220. BUG_ON(!trans);
  221. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  222. 0, &objectid);
  223. if (ret)
  224. goto fail;
  225. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  226. 0, objectid, NULL, 0, 0, 0);
  227. if (IS_ERR(leaf)) {
  228. ret = PTR_ERR(leaf);
  229. goto fail;
  230. }
  231. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  232. btrfs_set_header_bytenr(leaf, leaf->start);
  233. btrfs_set_header_generation(leaf, trans->transid);
  234. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  235. btrfs_set_header_owner(leaf, objectid);
  236. write_extent_buffer(leaf, root->fs_info->fsid,
  237. (unsigned long)btrfs_header_fsid(leaf),
  238. BTRFS_FSID_SIZE);
  239. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  240. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  241. BTRFS_UUID_SIZE);
  242. btrfs_mark_buffer_dirty(leaf);
  243. inode_item = &root_item.inode;
  244. memset(inode_item, 0, sizeof(*inode_item));
  245. inode_item->generation = cpu_to_le64(1);
  246. inode_item->size = cpu_to_le64(3);
  247. inode_item->nlink = cpu_to_le32(1);
  248. inode_item->nbytes = cpu_to_le64(root->leafsize);
  249. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  250. btrfs_set_root_bytenr(&root_item, leaf->start);
  251. btrfs_set_root_generation(&root_item, trans->transid);
  252. btrfs_set_root_level(&root_item, 0);
  253. btrfs_set_root_refs(&root_item, 1);
  254. btrfs_set_root_used(&root_item, leaf->len);
  255. btrfs_set_root_last_snapshot(&root_item, 0);
  256. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  257. root_item.drop_level = 0;
  258. btrfs_tree_unlock(leaf);
  259. free_extent_buffer(leaf);
  260. leaf = NULL;
  261. btrfs_set_root_dirid(&root_item, new_dirid);
  262. key.objectid = objectid;
  263. key.offset = 0;
  264. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  265. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  266. &root_item);
  267. if (ret)
  268. goto fail;
  269. key.offset = (u64)-1;
  270. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  271. BUG_ON(IS_ERR(new_root));
  272. btrfs_record_root_in_trans(trans, new_root);
  273. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  274. BTRFS_I(dir)->block_group);
  275. /*
  276. * insert the directory item
  277. */
  278. ret = btrfs_set_inode_index(dir, &index);
  279. BUG_ON(ret);
  280. ret = btrfs_insert_dir_item(trans, root,
  281. name, namelen, dir->i_ino, &key,
  282. BTRFS_FT_DIR, index);
  283. if (ret)
  284. goto fail;
  285. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  286. ret = btrfs_update_inode(trans, root, dir);
  287. BUG_ON(ret);
  288. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  289. objectid, root->root_key.objectid,
  290. dir->i_ino, index, name, namelen);
  291. BUG_ON(ret);
  292. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  293. fail:
  294. err = btrfs_commit_transaction(trans, root);
  295. if (err && !ret)
  296. ret = err;
  297. btrfs_unreserve_metadata_space(root, 6);
  298. return ret;
  299. }
  300. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  301. char *name, int namelen)
  302. {
  303. struct inode *inode;
  304. struct btrfs_pending_snapshot *pending_snapshot;
  305. struct btrfs_trans_handle *trans;
  306. int ret;
  307. if (!root->ref_cows)
  308. return -EINVAL;
  309. /*
  310. * 1 - inode item
  311. * 2 - refs
  312. * 1 - root item
  313. * 2 - dir items
  314. */
  315. ret = btrfs_reserve_metadata_space(root, 6);
  316. if (ret)
  317. goto fail;
  318. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  319. if (!pending_snapshot) {
  320. ret = -ENOMEM;
  321. btrfs_unreserve_metadata_space(root, 6);
  322. goto fail;
  323. }
  324. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  325. if (!pending_snapshot->name) {
  326. ret = -ENOMEM;
  327. kfree(pending_snapshot);
  328. btrfs_unreserve_metadata_space(root, 6);
  329. goto fail;
  330. }
  331. memcpy(pending_snapshot->name, name, namelen);
  332. pending_snapshot->name[namelen] = '\0';
  333. pending_snapshot->dentry = dentry;
  334. trans = btrfs_start_transaction(root, 1);
  335. BUG_ON(!trans);
  336. pending_snapshot->root = root;
  337. list_add(&pending_snapshot->list,
  338. &trans->transaction->pending_snapshots);
  339. ret = btrfs_commit_transaction(trans, root);
  340. BUG_ON(ret);
  341. btrfs_unreserve_metadata_space(root, 6);
  342. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  343. if (IS_ERR(inode)) {
  344. ret = PTR_ERR(inode);
  345. goto fail;
  346. }
  347. BUG_ON(!inode);
  348. d_instantiate(dentry, inode);
  349. ret = 0;
  350. fail:
  351. return ret;
  352. }
  353. /* copy of may_create in fs/namei.c() */
  354. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  355. {
  356. if (child->d_inode)
  357. return -EEXIST;
  358. if (IS_DEADDIR(dir))
  359. return -ENOENT;
  360. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  361. }
  362. /*
  363. * Create a new subvolume below @parent. This is largely modeled after
  364. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  365. * inside this filesystem so it's quite a bit simpler.
  366. */
  367. static noinline int btrfs_mksubvol(struct path *parent,
  368. char *name, int namelen,
  369. struct btrfs_root *snap_src)
  370. {
  371. struct inode *dir = parent->dentry->d_inode;
  372. struct dentry *dentry;
  373. int error;
  374. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  375. dentry = lookup_one_len(name, parent->dentry, namelen);
  376. error = PTR_ERR(dentry);
  377. if (IS_ERR(dentry))
  378. goto out_unlock;
  379. error = -EEXIST;
  380. if (dentry->d_inode)
  381. goto out_dput;
  382. error = mnt_want_write(parent->mnt);
  383. if (error)
  384. goto out_dput;
  385. error = btrfs_may_create(dir, dentry);
  386. if (error)
  387. goto out_drop_write;
  388. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  389. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  390. goto out_up_read;
  391. if (snap_src) {
  392. error = create_snapshot(snap_src, dentry,
  393. name, namelen);
  394. } else {
  395. error = create_subvol(BTRFS_I(dir)->root, dentry,
  396. name, namelen);
  397. }
  398. if (!error)
  399. fsnotify_mkdir(dir, dentry);
  400. out_up_read:
  401. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  402. out_drop_write:
  403. mnt_drop_write(parent->mnt);
  404. out_dput:
  405. dput(dentry);
  406. out_unlock:
  407. mutex_unlock(&dir->i_mutex);
  408. return error;
  409. }
  410. static int btrfs_defrag_file(struct file *file)
  411. {
  412. struct inode *inode = fdentry(file)->d_inode;
  413. struct btrfs_root *root = BTRFS_I(inode)->root;
  414. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  415. struct btrfs_ordered_extent *ordered;
  416. struct page *page;
  417. unsigned long last_index;
  418. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  419. unsigned long total_read = 0;
  420. u64 page_start;
  421. u64 page_end;
  422. unsigned long i;
  423. int ret;
  424. ret = btrfs_check_data_free_space(root, inode, inode->i_size);
  425. if (ret)
  426. return -ENOSPC;
  427. mutex_lock(&inode->i_mutex);
  428. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  429. for (i = 0; i <= last_index; i++) {
  430. if (total_read % ra_pages == 0) {
  431. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  432. min(last_index, i + ra_pages - 1));
  433. }
  434. total_read++;
  435. again:
  436. page = grab_cache_page(inode->i_mapping, i);
  437. if (!page)
  438. goto out_unlock;
  439. if (!PageUptodate(page)) {
  440. btrfs_readpage(NULL, page);
  441. lock_page(page);
  442. if (!PageUptodate(page)) {
  443. unlock_page(page);
  444. page_cache_release(page);
  445. goto out_unlock;
  446. }
  447. }
  448. wait_on_page_writeback(page);
  449. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  450. page_end = page_start + PAGE_CACHE_SIZE - 1;
  451. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  452. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  453. if (ordered) {
  454. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  455. unlock_page(page);
  456. page_cache_release(page);
  457. btrfs_start_ordered_extent(inode, ordered, 1);
  458. btrfs_put_ordered_extent(ordered);
  459. goto again;
  460. }
  461. set_page_extent_mapped(page);
  462. /*
  463. * this makes sure page_mkwrite is called on the
  464. * page if it is dirtied again later
  465. */
  466. clear_page_dirty_for_io(page);
  467. btrfs_set_extent_delalloc(inode, page_start, page_end);
  468. set_page_dirty(page);
  469. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  470. unlock_page(page);
  471. page_cache_release(page);
  472. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  473. }
  474. out_unlock:
  475. mutex_unlock(&inode->i_mutex);
  476. return 0;
  477. }
  478. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  479. void __user *arg)
  480. {
  481. u64 new_size;
  482. u64 old_size;
  483. u64 devid = 1;
  484. struct btrfs_ioctl_vol_args *vol_args;
  485. struct btrfs_trans_handle *trans;
  486. struct btrfs_device *device = NULL;
  487. char *sizestr;
  488. char *devstr = NULL;
  489. int ret = 0;
  490. int namelen;
  491. int mod = 0;
  492. if (root->fs_info->sb->s_flags & MS_RDONLY)
  493. return -EROFS;
  494. if (!capable(CAP_SYS_ADMIN))
  495. return -EPERM;
  496. vol_args = memdup_user(arg, sizeof(*vol_args));
  497. if (IS_ERR(vol_args))
  498. return PTR_ERR(vol_args);
  499. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  500. namelen = strlen(vol_args->name);
  501. mutex_lock(&root->fs_info->volume_mutex);
  502. sizestr = vol_args->name;
  503. devstr = strchr(sizestr, ':');
  504. if (devstr) {
  505. char *end;
  506. sizestr = devstr + 1;
  507. *devstr = '\0';
  508. devstr = vol_args->name;
  509. devid = simple_strtoull(devstr, &end, 10);
  510. printk(KERN_INFO "resizing devid %llu\n",
  511. (unsigned long long)devid);
  512. }
  513. device = btrfs_find_device(root, devid, NULL, NULL);
  514. if (!device) {
  515. printk(KERN_INFO "resizer unable to find device %llu\n",
  516. (unsigned long long)devid);
  517. ret = -EINVAL;
  518. goto out_unlock;
  519. }
  520. if (!strcmp(sizestr, "max"))
  521. new_size = device->bdev->bd_inode->i_size;
  522. else {
  523. if (sizestr[0] == '-') {
  524. mod = -1;
  525. sizestr++;
  526. } else if (sizestr[0] == '+') {
  527. mod = 1;
  528. sizestr++;
  529. }
  530. new_size = btrfs_parse_size(sizestr);
  531. if (new_size == 0) {
  532. ret = -EINVAL;
  533. goto out_unlock;
  534. }
  535. }
  536. old_size = device->total_bytes;
  537. if (mod < 0) {
  538. if (new_size > old_size) {
  539. ret = -EINVAL;
  540. goto out_unlock;
  541. }
  542. new_size = old_size - new_size;
  543. } else if (mod > 0) {
  544. new_size = old_size + new_size;
  545. }
  546. if (new_size < 256 * 1024 * 1024) {
  547. ret = -EINVAL;
  548. goto out_unlock;
  549. }
  550. if (new_size > device->bdev->bd_inode->i_size) {
  551. ret = -EFBIG;
  552. goto out_unlock;
  553. }
  554. do_div(new_size, root->sectorsize);
  555. new_size *= root->sectorsize;
  556. printk(KERN_INFO "new size for %s is %llu\n",
  557. device->name, (unsigned long long)new_size);
  558. if (new_size > old_size) {
  559. trans = btrfs_start_transaction(root, 1);
  560. ret = btrfs_grow_device(trans, device, new_size);
  561. btrfs_commit_transaction(trans, root);
  562. } else {
  563. ret = btrfs_shrink_device(device, new_size);
  564. }
  565. out_unlock:
  566. mutex_unlock(&root->fs_info->volume_mutex);
  567. kfree(vol_args);
  568. return ret;
  569. }
  570. static noinline int btrfs_ioctl_snap_create(struct file *file,
  571. void __user *arg, int subvol)
  572. {
  573. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  574. struct btrfs_ioctl_vol_args *vol_args;
  575. struct file *src_file;
  576. int namelen;
  577. int ret = 0;
  578. if (root->fs_info->sb->s_flags & MS_RDONLY)
  579. return -EROFS;
  580. vol_args = memdup_user(arg, sizeof(*vol_args));
  581. if (IS_ERR(vol_args))
  582. return PTR_ERR(vol_args);
  583. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  584. namelen = strlen(vol_args->name);
  585. if (strchr(vol_args->name, '/')) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. if (subvol) {
  590. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  591. NULL);
  592. } else {
  593. struct inode *src_inode;
  594. src_file = fget(vol_args->fd);
  595. if (!src_file) {
  596. ret = -EINVAL;
  597. goto out;
  598. }
  599. src_inode = src_file->f_path.dentry->d_inode;
  600. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  601. printk(KERN_INFO "btrfs: Snapshot src from "
  602. "another FS\n");
  603. ret = -EINVAL;
  604. fput(src_file);
  605. goto out;
  606. }
  607. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  608. BTRFS_I(src_inode)->root);
  609. fput(src_file);
  610. }
  611. out:
  612. kfree(vol_args);
  613. return ret;
  614. }
  615. /*
  616. * helper to check if the subvolume references other subvolumes
  617. */
  618. static noinline int may_destroy_subvol(struct btrfs_root *root)
  619. {
  620. struct btrfs_path *path;
  621. struct btrfs_key key;
  622. int ret;
  623. path = btrfs_alloc_path();
  624. if (!path)
  625. return -ENOMEM;
  626. key.objectid = root->root_key.objectid;
  627. key.type = BTRFS_ROOT_REF_KEY;
  628. key.offset = (u64)-1;
  629. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  630. &key, path, 0, 0);
  631. if (ret < 0)
  632. goto out;
  633. BUG_ON(ret == 0);
  634. ret = 0;
  635. if (path->slots[0] > 0) {
  636. path->slots[0]--;
  637. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  638. if (key.objectid == root->root_key.objectid &&
  639. key.type == BTRFS_ROOT_REF_KEY)
  640. ret = -ENOTEMPTY;
  641. }
  642. out:
  643. btrfs_free_path(path);
  644. return ret;
  645. }
  646. /*
  647. Search INODE_REFs to identify path name of 'dirid' directory
  648. in a 'tree_id' tree. and sets path name to 'name'.
  649. */
  650. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  651. u64 tree_id, u64 dirid, char *name)
  652. {
  653. struct btrfs_root *root;
  654. struct btrfs_key key;
  655. char *name_stack, *ptr;
  656. int ret = -1;
  657. int slot;
  658. int len;
  659. int total_len = 0;
  660. struct btrfs_inode_ref *iref;
  661. struct extent_buffer *l;
  662. struct btrfs_path *path;
  663. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  664. name[0]='\0';
  665. return 0;
  666. }
  667. path = btrfs_alloc_path();
  668. if (!path)
  669. return -ENOMEM;
  670. name_stack = kzalloc(BTRFS_PATH_NAME_MAX+1, GFP_NOFS);
  671. if (!name_stack) {
  672. btrfs_free_path(path);
  673. return -ENOMEM;
  674. }
  675. ptr = &name_stack[BTRFS_PATH_NAME_MAX];
  676. key.objectid = tree_id;
  677. key.type = BTRFS_ROOT_ITEM_KEY;
  678. key.offset = (u64)-1;
  679. root = btrfs_read_fs_root_no_name(info, &key);
  680. if (IS_ERR(root)) {
  681. printk(KERN_ERR "could not find root %llu\n", tree_id);
  682. return -ENOENT;
  683. }
  684. key.objectid = dirid;
  685. key.type = BTRFS_INODE_REF_KEY;
  686. key.offset = 0;
  687. while(1) {
  688. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  689. if (ret < 0)
  690. goto out;
  691. l = path->nodes[0];
  692. slot = path->slots[0];
  693. btrfs_item_key_to_cpu(l, &key, slot);
  694. if (ret > 0 && (key.objectid != dirid ||
  695. key.type != BTRFS_INODE_REF_KEY))
  696. goto out;
  697. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  698. len = btrfs_inode_ref_name_len(l, iref);
  699. ptr -= len + 1;
  700. total_len += len + 1;
  701. if (ptr < name_stack)
  702. goto out;
  703. *(ptr + len) = '/';
  704. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  705. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  706. break;
  707. btrfs_release_path(root, path);
  708. key.objectid = key.offset;
  709. key.offset = 0;
  710. dirid = key.objectid;
  711. }
  712. if (ptr < name_stack)
  713. goto out;
  714. strncpy(name, ptr, total_len);
  715. name[total_len]='\0';
  716. ret = 0;
  717. out:
  718. btrfs_free_path(path);
  719. kfree(name_stack);
  720. return ret;
  721. }
  722. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  723. void __user *arg)
  724. {
  725. struct dentry *parent = fdentry(file);
  726. struct dentry *dentry;
  727. struct inode *dir = parent->d_inode;
  728. struct inode *inode;
  729. struct btrfs_root *root = BTRFS_I(dir)->root;
  730. struct btrfs_root *dest = NULL;
  731. struct btrfs_ioctl_vol_args *vol_args;
  732. struct btrfs_trans_handle *trans;
  733. int namelen;
  734. int ret;
  735. int err = 0;
  736. if (!capable(CAP_SYS_ADMIN))
  737. return -EPERM;
  738. vol_args = memdup_user(arg, sizeof(*vol_args));
  739. if (IS_ERR(vol_args))
  740. return PTR_ERR(vol_args);
  741. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  742. namelen = strlen(vol_args->name);
  743. if (strchr(vol_args->name, '/') ||
  744. strncmp(vol_args->name, "..", namelen) == 0) {
  745. err = -EINVAL;
  746. goto out;
  747. }
  748. err = mnt_want_write(file->f_path.mnt);
  749. if (err)
  750. goto out;
  751. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  752. dentry = lookup_one_len(vol_args->name, parent, namelen);
  753. if (IS_ERR(dentry)) {
  754. err = PTR_ERR(dentry);
  755. goto out_unlock_dir;
  756. }
  757. if (!dentry->d_inode) {
  758. err = -ENOENT;
  759. goto out_dput;
  760. }
  761. inode = dentry->d_inode;
  762. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  763. err = -EINVAL;
  764. goto out_dput;
  765. }
  766. dest = BTRFS_I(inode)->root;
  767. mutex_lock(&inode->i_mutex);
  768. err = d_invalidate(dentry);
  769. if (err)
  770. goto out_unlock;
  771. down_write(&root->fs_info->subvol_sem);
  772. err = may_destroy_subvol(dest);
  773. if (err)
  774. goto out_up_write;
  775. trans = btrfs_start_transaction(root, 1);
  776. ret = btrfs_unlink_subvol(trans, root, dir,
  777. dest->root_key.objectid,
  778. dentry->d_name.name,
  779. dentry->d_name.len);
  780. BUG_ON(ret);
  781. btrfs_record_root_in_trans(trans, dest);
  782. memset(&dest->root_item.drop_progress, 0,
  783. sizeof(dest->root_item.drop_progress));
  784. dest->root_item.drop_level = 0;
  785. btrfs_set_root_refs(&dest->root_item, 0);
  786. ret = btrfs_insert_orphan_item(trans,
  787. root->fs_info->tree_root,
  788. dest->root_key.objectid);
  789. BUG_ON(ret);
  790. ret = btrfs_commit_transaction(trans, root);
  791. BUG_ON(ret);
  792. inode->i_flags |= S_DEAD;
  793. out_up_write:
  794. up_write(&root->fs_info->subvol_sem);
  795. out_unlock:
  796. mutex_unlock(&inode->i_mutex);
  797. if (!err) {
  798. shrink_dcache_sb(root->fs_info->sb);
  799. btrfs_invalidate_inodes(dest);
  800. d_delete(dentry);
  801. }
  802. out_dput:
  803. dput(dentry);
  804. out_unlock_dir:
  805. mutex_unlock(&dir->i_mutex);
  806. mnt_drop_write(file->f_path.mnt);
  807. out:
  808. kfree(vol_args);
  809. return err;
  810. }
  811. static int btrfs_ioctl_defrag(struct file *file)
  812. {
  813. struct inode *inode = fdentry(file)->d_inode;
  814. struct btrfs_root *root = BTRFS_I(inode)->root;
  815. int ret;
  816. ret = mnt_want_write(file->f_path.mnt);
  817. if (ret)
  818. return ret;
  819. switch (inode->i_mode & S_IFMT) {
  820. case S_IFDIR:
  821. if (!capable(CAP_SYS_ADMIN)) {
  822. ret = -EPERM;
  823. goto out;
  824. }
  825. btrfs_defrag_root(root, 0);
  826. btrfs_defrag_root(root->fs_info->extent_root, 0);
  827. break;
  828. case S_IFREG:
  829. if (!(file->f_mode & FMODE_WRITE)) {
  830. ret = -EINVAL;
  831. goto out;
  832. }
  833. btrfs_defrag_file(file);
  834. break;
  835. }
  836. out:
  837. mnt_drop_write(file->f_path.mnt);
  838. return ret;
  839. }
  840. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  841. {
  842. struct btrfs_ioctl_vol_args *vol_args;
  843. int ret;
  844. if (!capable(CAP_SYS_ADMIN))
  845. return -EPERM;
  846. vol_args = memdup_user(arg, sizeof(*vol_args));
  847. if (IS_ERR(vol_args))
  848. return PTR_ERR(vol_args);
  849. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  850. ret = btrfs_init_new_device(root, vol_args->name);
  851. kfree(vol_args);
  852. return ret;
  853. }
  854. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  855. {
  856. struct btrfs_ioctl_vol_args *vol_args;
  857. int ret;
  858. if (!capable(CAP_SYS_ADMIN))
  859. return -EPERM;
  860. if (root->fs_info->sb->s_flags & MS_RDONLY)
  861. return -EROFS;
  862. vol_args = memdup_user(arg, sizeof(*vol_args));
  863. if (IS_ERR(vol_args))
  864. return PTR_ERR(vol_args);
  865. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  866. ret = btrfs_rm_device(root, vol_args->name);
  867. kfree(vol_args);
  868. return ret;
  869. }
  870. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  871. u64 off, u64 olen, u64 destoff)
  872. {
  873. struct inode *inode = fdentry(file)->d_inode;
  874. struct btrfs_root *root = BTRFS_I(inode)->root;
  875. struct file *src_file;
  876. struct inode *src;
  877. struct btrfs_trans_handle *trans;
  878. struct btrfs_path *path;
  879. struct extent_buffer *leaf;
  880. char *buf;
  881. struct btrfs_key key;
  882. u32 nritems;
  883. int slot;
  884. int ret;
  885. u64 len = olen;
  886. u64 bs = root->fs_info->sb->s_blocksize;
  887. u64 hint_byte;
  888. /*
  889. * TODO:
  890. * - split compressed inline extents. annoying: we need to
  891. * decompress into destination's address_space (the file offset
  892. * may change, so source mapping won't do), then recompress (or
  893. * otherwise reinsert) a subrange.
  894. * - allow ranges within the same file to be cloned (provided
  895. * they don't overlap)?
  896. */
  897. /* the destination must be opened for writing */
  898. if (!(file->f_mode & FMODE_WRITE))
  899. return -EINVAL;
  900. ret = mnt_want_write(file->f_path.mnt);
  901. if (ret)
  902. return ret;
  903. src_file = fget(srcfd);
  904. if (!src_file) {
  905. ret = -EBADF;
  906. goto out_drop_write;
  907. }
  908. src = src_file->f_dentry->d_inode;
  909. ret = -EINVAL;
  910. if (src == inode)
  911. goto out_fput;
  912. ret = -EISDIR;
  913. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  914. goto out_fput;
  915. ret = -EXDEV;
  916. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  917. goto out_fput;
  918. ret = -ENOMEM;
  919. buf = vmalloc(btrfs_level_size(root, 0));
  920. if (!buf)
  921. goto out_fput;
  922. path = btrfs_alloc_path();
  923. if (!path) {
  924. vfree(buf);
  925. goto out_fput;
  926. }
  927. path->reada = 2;
  928. if (inode < src) {
  929. mutex_lock(&inode->i_mutex);
  930. mutex_lock(&src->i_mutex);
  931. } else {
  932. mutex_lock(&src->i_mutex);
  933. mutex_lock(&inode->i_mutex);
  934. }
  935. /* determine range to clone */
  936. ret = -EINVAL;
  937. if (off >= src->i_size || off + len > src->i_size)
  938. goto out_unlock;
  939. if (len == 0)
  940. olen = len = src->i_size - off;
  941. /* if we extend to eof, continue to block boundary */
  942. if (off + len == src->i_size)
  943. len = ((src->i_size + bs-1) & ~(bs-1))
  944. - off;
  945. /* verify the end result is block aligned */
  946. if ((off & (bs-1)) ||
  947. ((off + len) & (bs-1)))
  948. goto out_unlock;
  949. /* do any pending delalloc/csum calc on src, one way or
  950. another, and lock file content */
  951. while (1) {
  952. struct btrfs_ordered_extent *ordered;
  953. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  954. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  955. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  956. break;
  957. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  958. if (ordered)
  959. btrfs_put_ordered_extent(ordered);
  960. btrfs_wait_ordered_range(src, off, off+len);
  961. }
  962. trans = btrfs_start_transaction(root, 1);
  963. BUG_ON(!trans);
  964. /* punch hole in destination first */
  965. btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
  966. /* clone data */
  967. key.objectid = src->i_ino;
  968. key.type = BTRFS_EXTENT_DATA_KEY;
  969. key.offset = 0;
  970. while (1) {
  971. /*
  972. * note the key will change type as we walk through the
  973. * tree.
  974. */
  975. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  976. if (ret < 0)
  977. goto out;
  978. nritems = btrfs_header_nritems(path->nodes[0]);
  979. if (path->slots[0] >= nritems) {
  980. ret = btrfs_next_leaf(root, path);
  981. if (ret < 0)
  982. goto out;
  983. if (ret > 0)
  984. break;
  985. nritems = btrfs_header_nritems(path->nodes[0]);
  986. }
  987. leaf = path->nodes[0];
  988. slot = path->slots[0];
  989. btrfs_item_key_to_cpu(leaf, &key, slot);
  990. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  991. key.objectid != src->i_ino)
  992. break;
  993. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  994. struct btrfs_file_extent_item *extent;
  995. int type;
  996. u32 size;
  997. struct btrfs_key new_key;
  998. u64 disko = 0, diskl = 0;
  999. u64 datao = 0, datal = 0;
  1000. u8 comp;
  1001. size = btrfs_item_size_nr(leaf, slot);
  1002. read_extent_buffer(leaf, buf,
  1003. btrfs_item_ptr_offset(leaf, slot),
  1004. size);
  1005. extent = btrfs_item_ptr(leaf, slot,
  1006. struct btrfs_file_extent_item);
  1007. comp = btrfs_file_extent_compression(leaf, extent);
  1008. type = btrfs_file_extent_type(leaf, extent);
  1009. if (type == BTRFS_FILE_EXTENT_REG ||
  1010. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1011. disko = btrfs_file_extent_disk_bytenr(leaf,
  1012. extent);
  1013. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1014. extent);
  1015. datao = btrfs_file_extent_offset(leaf, extent);
  1016. datal = btrfs_file_extent_num_bytes(leaf,
  1017. extent);
  1018. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1019. /* take upper bound, may be compressed */
  1020. datal = btrfs_file_extent_ram_bytes(leaf,
  1021. extent);
  1022. }
  1023. btrfs_release_path(root, path);
  1024. if (key.offset + datal < off ||
  1025. key.offset >= off+len)
  1026. goto next;
  1027. memcpy(&new_key, &key, sizeof(new_key));
  1028. new_key.objectid = inode->i_ino;
  1029. new_key.offset = key.offset + destoff - off;
  1030. if (type == BTRFS_FILE_EXTENT_REG ||
  1031. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1032. ret = btrfs_insert_empty_item(trans, root, path,
  1033. &new_key, size);
  1034. if (ret)
  1035. goto out;
  1036. leaf = path->nodes[0];
  1037. slot = path->slots[0];
  1038. write_extent_buffer(leaf, buf,
  1039. btrfs_item_ptr_offset(leaf, slot),
  1040. size);
  1041. extent = btrfs_item_ptr(leaf, slot,
  1042. struct btrfs_file_extent_item);
  1043. if (off > key.offset) {
  1044. datao += off - key.offset;
  1045. datal -= off - key.offset;
  1046. }
  1047. if (key.offset + datal > off + len)
  1048. datal = off + len - key.offset;
  1049. /* disko == 0 means it's a hole */
  1050. if (!disko)
  1051. datao = 0;
  1052. btrfs_set_file_extent_offset(leaf, extent,
  1053. datao);
  1054. btrfs_set_file_extent_num_bytes(leaf, extent,
  1055. datal);
  1056. if (disko) {
  1057. inode_add_bytes(inode, datal);
  1058. ret = btrfs_inc_extent_ref(trans, root,
  1059. disko, diskl, 0,
  1060. root->root_key.objectid,
  1061. inode->i_ino,
  1062. new_key.offset - datao);
  1063. BUG_ON(ret);
  1064. }
  1065. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1066. u64 skip = 0;
  1067. u64 trim = 0;
  1068. if (off > key.offset) {
  1069. skip = off - key.offset;
  1070. new_key.offset += skip;
  1071. }
  1072. if (key.offset + datal > off+len)
  1073. trim = key.offset + datal - (off+len);
  1074. if (comp && (skip || trim)) {
  1075. ret = -EINVAL;
  1076. goto out;
  1077. }
  1078. size -= skip + trim;
  1079. datal -= skip + trim;
  1080. ret = btrfs_insert_empty_item(trans, root, path,
  1081. &new_key, size);
  1082. if (ret)
  1083. goto out;
  1084. if (skip) {
  1085. u32 start =
  1086. btrfs_file_extent_calc_inline_size(0);
  1087. memmove(buf+start, buf+start+skip,
  1088. datal);
  1089. }
  1090. leaf = path->nodes[0];
  1091. slot = path->slots[0];
  1092. write_extent_buffer(leaf, buf,
  1093. btrfs_item_ptr_offset(leaf, slot),
  1094. size);
  1095. inode_add_bytes(inode, datal);
  1096. }
  1097. btrfs_mark_buffer_dirty(leaf);
  1098. }
  1099. next:
  1100. btrfs_release_path(root, path);
  1101. key.offset++;
  1102. }
  1103. ret = 0;
  1104. out:
  1105. btrfs_release_path(root, path);
  1106. if (ret == 0) {
  1107. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1108. if (destoff + olen > inode->i_size)
  1109. btrfs_i_size_write(inode, destoff + olen);
  1110. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1111. ret = btrfs_update_inode(trans, root, inode);
  1112. }
  1113. btrfs_end_transaction(trans, root);
  1114. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1115. if (ret)
  1116. vmtruncate(inode, 0);
  1117. out_unlock:
  1118. mutex_unlock(&src->i_mutex);
  1119. mutex_unlock(&inode->i_mutex);
  1120. vfree(buf);
  1121. btrfs_free_path(path);
  1122. out_fput:
  1123. fput(src_file);
  1124. out_drop_write:
  1125. mnt_drop_write(file->f_path.mnt);
  1126. return ret;
  1127. }
  1128. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1129. {
  1130. struct btrfs_ioctl_clone_range_args args;
  1131. if (copy_from_user(&args, argp, sizeof(args)))
  1132. return -EFAULT;
  1133. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1134. args.src_length, args.dest_offset);
  1135. }
  1136. /*
  1137. * there are many ways the trans_start and trans_end ioctls can lead
  1138. * to deadlocks. They should only be used by applications that
  1139. * basically own the machine, and have a very in depth understanding
  1140. * of all the possible deadlocks and enospc problems.
  1141. */
  1142. static long btrfs_ioctl_trans_start(struct file *file)
  1143. {
  1144. struct inode *inode = fdentry(file)->d_inode;
  1145. struct btrfs_root *root = BTRFS_I(inode)->root;
  1146. struct btrfs_trans_handle *trans;
  1147. int ret;
  1148. ret = -EPERM;
  1149. if (!capable(CAP_SYS_ADMIN))
  1150. goto out;
  1151. ret = -EINPROGRESS;
  1152. if (file->private_data)
  1153. goto out;
  1154. ret = mnt_want_write(file->f_path.mnt);
  1155. if (ret)
  1156. goto out;
  1157. mutex_lock(&root->fs_info->trans_mutex);
  1158. root->fs_info->open_ioctl_trans++;
  1159. mutex_unlock(&root->fs_info->trans_mutex);
  1160. ret = -ENOMEM;
  1161. trans = btrfs_start_ioctl_transaction(root, 0);
  1162. if (!trans)
  1163. goto out_drop;
  1164. file->private_data = trans;
  1165. return 0;
  1166. out_drop:
  1167. mutex_lock(&root->fs_info->trans_mutex);
  1168. root->fs_info->open_ioctl_trans--;
  1169. mutex_unlock(&root->fs_info->trans_mutex);
  1170. mnt_drop_write(file->f_path.mnt);
  1171. out:
  1172. return ret;
  1173. }
  1174. /*
  1175. * there are many ways the trans_start and trans_end ioctls can lead
  1176. * to deadlocks. They should only be used by applications that
  1177. * basically own the machine, and have a very in depth understanding
  1178. * of all the possible deadlocks and enospc problems.
  1179. */
  1180. long btrfs_ioctl_trans_end(struct file *file)
  1181. {
  1182. struct inode *inode = fdentry(file)->d_inode;
  1183. struct btrfs_root *root = BTRFS_I(inode)->root;
  1184. struct btrfs_trans_handle *trans;
  1185. trans = file->private_data;
  1186. if (!trans)
  1187. return -EINVAL;
  1188. file->private_data = NULL;
  1189. btrfs_end_transaction(trans, root);
  1190. mutex_lock(&root->fs_info->trans_mutex);
  1191. root->fs_info->open_ioctl_trans--;
  1192. mutex_unlock(&root->fs_info->trans_mutex);
  1193. mnt_drop_write(file->f_path.mnt);
  1194. return 0;
  1195. }
  1196. long btrfs_ioctl(struct file *file, unsigned int
  1197. cmd, unsigned long arg)
  1198. {
  1199. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1200. void __user *argp = (void __user *)arg;
  1201. switch (cmd) {
  1202. case FS_IOC_GETFLAGS:
  1203. return btrfs_ioctl_getflags(file, argp);
  1204. case FS_IOC_SETFLAGS:
  1205. return btrfs_ioctl_setflags(file, argp);
  1206. case FS_IOC_GETVERSION:
  1207. return btrfs_ioctl_getversion(file, argp);
  1208. case BTRFS_IOC_SNAP_CREATE:
  1209. return btrfs_ioctl_snap_create(file, argp, 0);
  1210. case BTRFS_IOC_SUBVOL_CREATE:
  1211. return btrfs_ioctl_snap_create(file, argp, 1);
  1212. case BTRFS_IOC_SNAP_DESTROY:
  1213. return btrfs_ioctl_snap_destroy(file, argp);
  1214. case BTRFS_IOC_DEFRAG:
  1215. return btrfs_ioctl_defrag(file);
  1216. case BTRFS_IOC_RESIZE:
  1217. return btrfs_ioctl_resize(root, argp);
  1218. case BTRFS_IOC_ADD_DEV:
  1219. return btrfs_ioctl_add_dev(root, argp);
  1220. case BTRFS_IOC_RM_DEV:
  1221. return btrfs_ioctl_rm_dev(root, argp);
  1222. case BTRFS_IOC_BALANCE:
  1223. return btrfs_balance(root->fs_info->dev_root);
  1224. case BTRFS_IOC_CLONE:
  1225. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1226. case BTRFS_IOC_CLONE_RANGE:
  1227. return btrfs_ioctl_clone_range(file, argp);
  1228. case BTRFS_IOC_TRANS_START:
  1229. return btrfs_ioctl_trans_start(file);
  1230. case BTRFS_IOC_TRANS_END:
  1231. return btrfs_ioctl_trans_end(file);
  1232. case BTRFS_IOC_SYNC:
  1233. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1234. return 0;
  1235. }
  1236. return -ENOTTY;
  1237. }