ioctl.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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 <linux/slab.h>
  42. #include "compat.h"
  43. #include "ctree.h"
  44. #include "disk-io.h"
  45. #include "transaction.h"
  46. #include "btrfs_inode.h"
  47. #include "ioctl.h"
  48. #include "print-tree.h"
  49. #include "volumes.h"
  50. #include "locking.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. u64 *async_transid)
  198. {
  199. struct btrfs_trans_handle *trans;
  200. struct btrfs_key key;
  201. struct btrfs_root_item root_item;
  202. struct btrfs_inode_item *inode_item;
  203. struct extent_buffer *leaf;
  204. struct btrfs_root *new_root;
  205. struct dentry *parent = dget_parent(dentry);
  206. struct inode *dir;
  207. int ret;
  208. int err;
  209. u64 objectid;
  210. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  211. u64 index = 0;
  212. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  213. 0, &objectid);
  214. if (ret) {
  215. dput(parent);
  216. return ret;
  217. }
  218. dir = parent->d_inode;
  219. /*
  220. * 1 - inode item
  221. * 2 - refs
  222. * 1 - root item
  223. * 2 - dir items
  224. */
  225. trans = btrfs_start_transaction(root, 6);
  226. if (IS_ERR(trans)) {
  227. dput(parent);
  228. return PTR_ERR(trans);
  229. }
  230. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  231. 0, objectid, NULL, 0, 0, 0);
  232. if (IS_ERR(leaf)) {
  233. ret = PTR_ERR(leaf);
  234. goto fail;
  235. }
  236. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  237. btrfs_set_header_bytenr(leaf, leaf->start);
  238. btrfs_set_header_generation(leaf, trans->transid);
  239. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  240. btrfs_set_header_owner(leaf, objectid);
  241. write_extent_buffer(leaf, root->fs_info->fsid,
  242. (unsigned long)btrfs_header_fsid(leaf),
  243. BTRFS_FSID_SIZE);
  244. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  245. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  246. BTRFS_UUID_SIZE);
  247. btrfs_mark_buffer_dirty(leaf);
  248. inode_item = &root_item.inode;
  249. memset(inode_item, 0, sizeof(*inode_item));
  250. inode_item->generation = cpu_to_le64(1);
  251. inode_item->size = cpu_to_le64(3);
  252. inode_item->nlink = cpu_to_le32(1);
  253. inode_item->nbytes = cpu_to_le64(root->leafsize);
  254. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  255. btrfs_set_root_bytenr(&root_item, leaf->start);
  256. btrfs_set_root_generation(&root_item, trans->transid);
  257. btrfs_set_root_level(&root_item, 0);
  258. btrfs_set_root_refs(&root_item, 1);
  259. btrfs_set_root_used(&root_item, leaf->len);
  260. btrfs_set_root_last_snapshot(&root_item, 0);
  261. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  262. root_item.drop_level = 0;
  263. btrfs_tree_unlock(leaf);
  264. free_extent_buffer(leaf);
  265. leaf = NULL;
  266. btrfs_set_root_dirid(&root_item, new_dirid);
  267. key.objectid = objectid;
  268. key.offset = 0;
  269. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  270. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  271. &root_item);
  272. if (ret)
  273. goto fail;
  274. key.offset = (u64)-1;
  275. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  276. BUG_ON(IS_ERR(new_root));
  277. btrfs_record_root_in_trans(trans, new_root);
  278. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  279. BTRFS_I(dir)->block_group);
  280. /*
  281. * insert the directory item
  282. */
  283. ret = btrfs_set_inode_index(dir, &index);
  284. BUG_ON(ret);
  285. ret = btrfs_insert_dir_item(trans, root,
  286. name, namelen, dir->i_ino, &key,
  287. BTRFS_FT_DIR, index);
  288. if (ret)
  289. goto fail;
  290. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  291. ret = btrfs_update_inode(trans, root, dir);
  292. BUG_ON(ret);
  293. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  294. objectid, root->root_key.objectid,
  295. dir->i_ino, index, name, namelen);
  296. BUG_ON(ret);
  297. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  298. fail:
  299. dput(parent);
  300. if (async_transid) {
  301. *async_transid = trans->transid;
  302. err = btrfs_commit_transaction_async(trans, root, 1);
  303. } else {
  304. err = btrfs_commit_transaction(trans, root);
  305. }
  306. if (err && !ret)
  307. ret = err;
  308. return ret;
  309. }
  310. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  311. char *name, int namelen, u64 *async_transid)
  312. {
  313. struct inode *inode;
  314. struct dentry *parent;
  315. struct btrfs_pending_snapshot *pending_snapshot;
  316. struct btrfs_trans_handle *trans;
  317. int ret;
  318. if (!root->ref_cows)
  319. return -EINVAL;
  320. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  321. if (!pending_snapshot)
  322. return -ENOMEM;
  323. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  324. pending_snapshot->dentry = dentry;
  325. pending_snapshot->root = root;
  326. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  327. if (IS_ERR(trans)) {
  328. ret = PTR_ERR(trans);
  329. goto fail;
  330. }
  331. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  332. BUG_ON(ret);
  333. list_add(&pending_snapshot->list,
  334. &trans->transaction->pending_snapshots);
  335. if (async_transid) {
  336. *async_transid = trans->transid;
  337. ret = btrfs_commit_transaction_async(trans,
  338. root->fs_info->extent_root, 1);
  339. } else {
  340. ret = btrfs_commit_transaction(trans,
  341. root->fs_info->extent_root);
  342. }
  343. BUG_ON(ret);
  344. ret = pending_snapshot->error;
  345. if (ret)
  346. goto fail;
  347. btrfs_orphan_cleanup(pending_snapshot->snap);
  348. parent = dget_parent(dentry);
  349. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  350. dput(parent);
  351. if (IS_ERR(inode)) {
  352. ret = PTR_ERR(inode);
  353. goto fail;
  354. }
  355. BUG_ON(!inode);
  356. d_instantiate(dentry, inode);
  357. ret = 0;
  358. fail:
  359. kfree(pending_snapshot);
  360. return ret;
  361. }
  362. /* copy of check_sticky in fs/namei.c()
  363. * It's inline, so penalty for filesystems that don't use sticky bit is
  364. * minimal.
  365. */
  366. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  367. {
  368. uid_t fsuid = current_fsuid();
  369. if (!(dir->i_mode & S_ISVTX))
  370. return 0;
  371. if (inode->i_uid == fsuid)
  372. return 0;
  373. if (dir->i_uid == fsuid)
  374. return 0;
  375. return !capable(CAP_FOWNER);
  376. }
  377. /* copy of may_delete in fs/namei.c()
  378. * Check whether we can remove a link victim from directory dir, check
  379. * whether the type of victim is right.
  380. * 1. We can't do it if dir is read-only (done in permission())
  381. * 2. We should have write and exec permissions on dir
  382. * 3. We can't remove anything from append-only dir
  383. * 4. We can't do anything with immutable dir (done in permission())
  384. * 5. If the sticky bit on dir is set we should either
  385. * a. be owner of dir, or
  386. * b. be owner of victim, or
  387. * c. have CAP_FOWNER capability
  388. * 6. If the victim is append-only or immutable we can't do antyhing with
  389. * links pointing to it.
  390. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  391. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  392. * 9. We can't remove a root or mountpoint.
  393. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  394. * nfs_async_unlink().
  395. */
  396. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  397. {
  398. int error;
  399. if (!victim->d_inode)
  400. return -ENOENT;
  401. BUG_ON(victim->d_parent->d_inode != dir);
  402. audit_inode_child(victim, dir);
  403. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  404. if (error)
  405. return error;
  406. if (IS_APPEND(dir))
  407. return -EPERM;
  408. if (btrfs_check_sticky(dir, victim->d_inode)||
  409. IS_APPEND(victim->d_inode)||
  410. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  411. return -EPERM;
  412. if (isdir) {
  413. if (!S_ISDIR(victim->d_inode->i_mode))
  414. return -ENOTDIR;
  415. if (IS_ROOT(victim))
  416. return -EBUSY;
  417. } else if (S_ISDIR(victim->d_inode->i_mode))
  418. return -EISDIR;
  419. if (IS_DEADDIR(dir))
  420. return -ENOENT;
  421. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  422. return -EBUSY;
  423. return 0;
  424. }
  425. /* copy of may_create in fs/namei.c() */
  426. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  427. {
  428. if (child->d_inode)
  429. return -EEXIST;
  430. if (IS_DEADDIR(dir))
  431. return -ENOENT;
  432. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  433. }
  434. /*
  435. * Create a new subvolume below @parent. This is largely modeled after
  436. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  437. * inside this filesystem so it's quite a bit simpler.
  438. */
  439. static noinline int btrfs_mksubvol(struct path *parent,
  440. char *name, int namelen,
  441. struct btrfs_root *snap_src,
  442. u64 *async_transid)
  443. {
  444. struct inode *dir = parent->dentry->d_inode;
  445. struct dentry *dentry;
  446. int error;
  447. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  448. dentry = lookup_one_len(name, parent->dentry, namelen);
  449. error = PTR_ERR(dentry);
  450. if (IS_ERR(dentry))
  451. goto out_unlock;
  452. error = -EEXIST;
  453. if (dentry->d_inode)
  454. goto out_dput;
  455. error = mnt_want_write(parent->mnt);
  456. if (error)
  457. goto out_dput;
  458. error = btrfs_may_create(dir, dentry);
  459. if (error)
  460. goto out_drop_write;
  461. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  462. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  463. goto out_up_read;
  464. if (snap_src) {
  465. error = create_snapshot(snap_src, dentry,
  466. name, namelen, async_transid);
  467. } else {
  468. error = create_subvol(BTRFS_I(dir)->root, dentry,
  469. name, namelen, async_transid);
  470. }
  471. if (!error)
  472. fsnotify_mkdir(dir, dentry);
  473. out_up_read:
  474. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  475. out_drop_write:
  476. mnt_drop_write(parent->mnt);
  477. out_dput:
  478. dput(dentry);
  479. out_unlock:
  480. mutex_unlock(&dir->i_mutex);
  481. return error;
  482. }
  483. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  484. int thresh, u64 *last_len, u64 *skip,
  485. u64 *defrag_end)
  486. {
  487. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  488. struct extent_map *em = NULL;
  489. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  490. int ret = 1;
  491. if (thresh == 0)
  492. thresh = 256 * 1024;
  493. /*
  494. * make sure that once we start defragging and extent, we keep on
  495. * defragging it
  496. */
  497. if (start < *defrag_end)
  498. return 1;
  499. *skip = 0;
  500. /*
  501. * hopefully we have this extent in the tree already, try without
  502. * the full extent lock
  503. */
  504. read_lock(&em_tree->lock);
  505. em = lookup_extent_mapping(em_tree, start, len);
  506. read_unlock(&em_tree->lock);
  507. if (!em) {
  508. /* get the big lock and read metadata off disk */
  509. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  510. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  511. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  512. if (IS_ERR(em))
  513. return 0;
  514. }
  515. /* this will cover holes, and inline extents */
  516. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  517. ret = 0;
  518. /*
  519. * we hit a real extent, if it is big don't bother defragging it again
  520. */
  521. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  522. ret = 0;
  523. /*
  524. * last_len ends up being a counter of how many bytes we've defragged.
  525. * every time we choose not to defrag an extent, we reset *last_len
  526. * so that the next tiny extent will force a defrag.
  527. *
  528. * The end result of this is that tiny extents before a single big
  529. * extent will force at least part of that big extent to be defragged.
  530. */
  531. if (ret) {
  532. *last_len += len;
  533. *defrag_end = extent_map_end(em);
  534. } else {
  535. *last_len = 0;
  536. *skip = extent_map_end(em);
  537. *defrag_end = 0;
  538. }
  539. free_extent_map(em);
  540. return ret;
  541. }
  542. static int btrfs_defrag_file(struct file *file,
  543. struct btrfs_ioctl_defrag_range_args *range)
  544. {
  545. struct inode *inode = fdentry(file)->d_inode;
  546. struct btrfs_root *root = BTRFS_I(inode)->root;
  547. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  548. struct btrfs_ordered_extent *ordered;
  549. struct page *page;
  550. struct btrfs_super_block *disk_super;
  551. unsigned long last_index;
  552. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  553. unsigned long total_read = 0;
  554. u64 features;
  555. u64 page_start;
  556. u64 page_end;
  557. u64 last_len = 0;
  558. u64 skip = 0;
  559. u64 defrag_end = 0;
  560. unsigned long i;
  561. int ret;
  562. int compress_type = BTRFS_COMPRESS_ZLIB;
  563. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  564. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  565. return -EINVAL;
  566. if (range->compress_type)
  567. compress_type = range->compress_type;
  568. }
  569. if (inode->i_size == 0)
  570. return 0;
  571. if (range->start + range->len > range->start) {
  572. last_index = min_t(u64, inode->i_size - 1,
  573. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  574. } else {
  575. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  576. }
  577. i = range->start >> PAGE_CACHE_SHIFT;
  578. while (i <= last_index) {
  579. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  580. PAGE_CACHE_SIZE,
  581. range->extent_thresh,
  582. &last_len, &skip,
  583. &defrag_end)) {
  584. unsigned long next;
  585. /*
  586. * the should_defrag function tells us how much to skip
  587. * bump our counter by the suggested amount
  588. */
  589. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  590. i = max(i + 1, next);
  591. continue;
  592. }
  593. if (total_read % ra_pages == 0) {
  594. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  595. min(last_index, i + ra_pages - 1));
  596. }
  597. total_read++;
  598. mutex_lock(&inode->i_mutex);
  599. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  600. BTRFS_I(inode)->force_compress = compress_type;
  601. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  602. if (ret)
  603. goto err_unlock;
  604. again:
  605. if (inode->i_size == 0 ||
  606. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  607. ret = 0;
  608. goto err_reservations;
  609. }
  610. page = grab_cache_page(inode->i_mapping, i);
  611. if (!page) {
  612. ret = -ENOMEM;
  613. goto err_reservations;
  614. }
  615. if (!PageUptodate(page)) {
  616. btrfs_readpage(NULL, page);
  617. lock_page(page);
  618. if (!PageUptodate(page)) {
  619. unlock_page(page);
  620. page_cache_release(page);
  621. ret = -EIO;
  622. goto err_reservations;
  623. }
  624. }
  625. if (page->mapping != inode->i_mapping) {
  626. unlock_page(page);
  627. page_cache_release(page);
  628. goto again;
  629. }
  630. wait_on_page_writeback(page);
  631. if (PageDirty(page)) {
  632. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  633. goto loop_unlock;
  634. }
  635. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  636. page_end = page_start + PAGE_CACHE_SIZE - 1;
  637. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  638. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  639. if (ordered) {
  640. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  641. unlock_page(page);
  642. page_cache_release(page);
  643. btrfs_start_ordered_extent(inode, ordered, 1);
  644. btrfs_put_ordered_extent(ordered);
  645. goto again;
  646. }
  647. set_page_extent_mapped(page);
  648. /*
  649. * this makes sure page_mkwrite is called on the
  650. * page if it is dirtied again later
  651. */
  652. clear_page_dirty_for_io(page);
  653. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  654. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  655. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  656. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  657. ClearPageChecked(page);
  658. set_page_dirty(page);
  659. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  660. loop_unlock:
  661. unlock_page(page);
  662. page_cache_release(page);
  663. mutex_unlock(&inode->i_mutex);
  664. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  665. i++;
  666. }
  667. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  668. filemap_flush(inode->i_mapping);
  669. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  670. /* the filemap_flush will queue IO into the worker threads, but
  671. * we have to make sure the IO is actually started and that
  672. * ordered extents get created before we return
  673. */
  674. atomic_inc(&root->fs_info->async_submit_draining);
  675. while (atomic_read(&root->fs_info->nr_async_submits) ||
  676. atomic_read(&root->fs_info->async_delalloc_pages)) {
  677. wait_event(root->fs_info->async_submit_wait,
  678. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  679. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  680. }
  681. atomic_dec(&root->fs_info->async_submit_draining);
  682. mutex_lock(&inode->i_mutex);
  683. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  684. mutex_unlock(&inode->i_mutex);
  685. }
  686. disk_super = &root->fs_info->super_copy;
  687. features = btrfs_super_incompat_flags(disk_super);
  688. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  689. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  690. btrfs_set_super_incompat_flags(disk_super, features);
  691. }
  692. return 0;
  693. err_reservations:
  694. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  695. err_unlock:
  696. mutex_unlock(&inode->i_mutex);
  697. return ret;
  698. }
  699. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  700. void __user *arg)
  701. {
  702. u64 new_size;
  703. u64 old_size;
  704. u64 devid = 1;
  705. struct btrfs_ioctl_vol_args *vol_args;
  706. struct btrfs_trans_handle *trans;
  707. struct btrfs_device *device = NULL;
  708. char *sizestr;
  709. char *devstr = NULL;
  710. int ret = 0;
  711. int mod = 0;
  712. if (root->fs_info->sb->s_flags & MS_RDONLY)
  713. return -EROFS;
  714. if (!capable(CAP_SYS_ADMIN))
  715. return -EPERM;
  716. vol_args = memdup_user(arg, sizeof(*vol_args));
  717. if (IS_ERR(vol_args))
  718. return PTR_ERR(vol_args);
  719. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  720. mutex_lock(&root->fs_info->volume_mutex);
  721. sizestr = vol_args->name;
  722. devstr = strchr(sizestr, ':');
  723. if (devstr) {
  724. char *end;
  725. sizestr = devstr + 1;
  726. *devstr = '\0';
  727. devstr = vol_args->name;
  728. devid = simple_strtoull(devstr, &end, 10);
  729. printk(KERN_INFO "resizing devid %llu\n",
  730. (unsigned long long)devid);
  731. }
  732. device = btrfs_find_device(root, devid, NULL, NULL);
  733. if (!device) {
  734. printk(KERN_INFO "resizer unable to find device %llu\n",
  735. (unsigned long long)devid);
  736. ret = -EINVAL;
  737. goto out_unlock;
  738. }
  739. if (!strcmp(sizestr, "max"))
  740. new_size = device->bdev->bd_inode->i_size;
  741. else {
  742. if (sizestr[0] == '-') {
  743. mod = -1;
  744. sizestr++;
  745. } else if (sizestr[0] == '+') {
  746. mod = 1;
  747. sizestr++;
  748. }
  749. new_size = memparse(sizestr, NULL);
  750. if (new_size == 0) {
  751. ret = -EINVAL;
  752. goto out_unlock;
  753. }
  754. }
  755. old_size = device->total_bytes;
  756. if (mod < 0) {
  757. if (new_size > old_size) {
  758. ret = -EINVAL;
  759. goto out_unlock;
  760. }
  761. new_size = old_size - new_size;
  762. } else if (mod > 0) {
  763. new_size = old_size + new_size;
  764. }
  765. if (new_size < 256 * 1024 * 1024) {
  766. ret = -EINVAL;
  767. goto out_unlock;
  768. }
  769. if (new_size > device->bdev->bd_inode->i_size) {
  770. ret = -EFBIG;
  771. goto out_unlock;
  772. }
  773. do_div(new_size, root->sectorsize);
  774. new_size *= root->sectorsize;
  775. printk(KERN_INFO "new size for %s is %llu\n",
  776. device->name, (unsigned long long)new_size);
  777. if (new_size > old_size) {
  778. trans = btrfs_start_transaction(root, 0);
  779. ret = btrfs_grow_device(trans, device, new_size);
  780. btrfs_commit_transaction(trans, root);
  781. } else {
  782. ret = btrfs_shrink_device(device, new_size);
  783. }
  784. out_unlock:
  785. mutex_unlock(&root->fs_info->volume_mutex);
  786. kfree(vol_args);
  787. return ret;
  788. }
  789. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  790. char *name,
  791. unsigned long fd,
  792. int subvol,
  793. u64 *transid)
  794. {
  795. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  796. struct file *src_file;
  797. int namelen;
  798. int ret = 0;
  799. if (root->fs_info->sb->s_flags & MS_RDONLY)
  800. return -EROFS;
  801. namelen = strlen(name);
  802. if (strchr(name, '/')) {
  803. ret = -EINVAL;
  804. goto out;
  805. }
  806. if (subvol) {
  807. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  808. NULL, transid);
  809. } else {
  810. struct inode *src_inode;
  811. src_file = fget(fd);
  812. if (!src_file) {
  813. ret = -EINVAL;
  814. goto out;
  815. }
  816. src_inode = src_file->f_path.dentry->d_inode;
  817. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  818. printk(KERN_INFO "btrfs: Snapshot src from "
  819. "another FS\n");
  820. ret = -EINVAL;
  821. fput(src_file);
  822. goto out;
  823. }
  824. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  825. BTRFS_I(src_inode)->root,
  826. transid);
  827. fput(src_file);
  828. }
  829. out:
  830. return ret;
  831. }
  832. static noinline int btrfs_ioctl_snap_create(struct file *file,
  833. void __user *arg, int subvol,
  834. int v2)
  835. {
  836. struct btrfs_ioctl_vol_args *vol_args = NULL;
  837. struct btrfs_ioctl_vol_args_v2 *vol_args_v2 = NULL;
  838. char *name;
  839. u64 fd;
  840. int ret;
  841. if (v2) {
  842. u64 transid = 0;
  843. u64 *ptr = NULL;
  844. vol_args_v2 = memdup_user(arg, sizeof(*vol_args_v2));
  845. if (IS_ERR(vol_args_v2))
  846. return PTR_ERR(vol_args_v2);
  847. if (vol_args_v2->flags & ~BTRFS_SUBVOL_CREATE_ASYNC) {
  848. ret = -EINVAL;
  849. goto out;
  850. }
  851. name = vol_args_v2->name;
  852. fd = vol_args_v2->fd;
  853. vol_args_v2->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  854. if (vol_args_v2->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  855. ptr = &transid;
  856. ret = btrfs_ioctl_snap_create_transid(file, name, fd,
  857. subvol, ptr);
  858. if (ret == 0 && ptr &&
  859. copy_to_user(arg +
  860. offsetof(struct btrfs_ioctl_vol_args_v2,
  861. transid), ptr, sizeof(*ptr)))
  862. ret = -EFAULT;
  863. } else {
  864. vol_args = memdup_user(arg, sizeof(*vol_args));
  865. if (IS_ERR(vol_args))
  866. return PTR_ERR(vol_args);
  867. name = vol_args->name;
  868. fd = vol_args->fd;
  869. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  870. ret = btrfs_ioctl_snap_create_transid(file, name, fd,
  871. subvol, NULL);
  872. }
  873. out:
  874. kfree(vol_args);
  875. kfree(vol_args_v2);
  876. return ret;
  877. }
  878. /*
  879. * helper to check if the subvolume references other subvolumes
  880. */
  881. static noinline int may_destroy_subvol(struct btrfs_root *root)
  882. {
  883. struct btrfs_path *path;
  884. struct btrfs_key key;
  885. int ret;
  886. path = btrfs_alloc_path();
  887. if (!path)
  888. return -ENOMEM;
  889. key.objectid = root->root_key.objectid;
  890. key.type = BTRFS_ROOT_REF_KEY;
  891. key.offset = (u64)-1;
  892. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  893. &key, path, 0, 0);
  894. if (ret < 0)
  895. goto out;
  896. BUG_ON(ret == 0);
  897. ret = 0;
  898. if (path->slots[0] > 0) {
  899. path->slots[0]--;
  900. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  901. if (key.objectid == root->root_key.objectid &&
  902. key.type == BTRFS_ROOT_REF_KEY)
  903. ret = -ENOTEMPTY;
  904. }
  905. out:
  906. btrfs_free_path(path);
  907. return ret;
  908. }
  909. static noinline int key_in_sk(struct btrfs_key *key,
  910. struct btrfs_ioctl_search_key *sk)
  911. {
  912. struct btrfs_key test;
  913. int ret;
  914. test.objectid = sk->min_objectid;
  915. test.type = sk->min_type;
  916. test.offset = sk->min_offset;
  917. ret = btrfs_comp_cpu_keys(key, &test);
  918. if (ret < 0)
  919. return 0;
  920. test.objectid = sk->max_objectid;
  921. test.type = sk->max_type;
  922. test.offset = sk->max_offset;
  923. ret = btrfs_comp_cpu_keys(key, &test);
  924. if (ret > 0)
  925. return 0;
  926. return 1;
  927. }
  928. static noinline int copy_to_sk(struct btrfs_root *root,
  929. struct btrfs_path *path,
  930. struct btrfs_key *key,
  931. struct btrfs_ioctl_search_key *sk,
  932. char *buf,
  933. unsigned long *sk_offset,
  934. int *num_found)
  935. {
  936. u64 found_transid;
  937. struct extent_buffer *leaf;
  938. struct btrfs_ioctl_search_header sh;
  939. unsigned long item_off;
  940. unsigned long item_len;
  941. int nritems;
  942. int i;
  943. int slot;
  944. int found = 0;
  945. int ret = 0;
  946. leaf = path->nodes[0];
  947. slot = path->slots[0];
  948. nritems = btrfs_header_nritems(leaf);
  949. if (btrfs_header_generation(leaf) > sk->max_transid) {
  950. i = nritems;
  951. goto advance_key;
  952. }
  953. found_transid = btrfs_header_generation(leaf);
  954. for (i = slot; i < nritems; i++) {
  955. item_off = btrfs_item_ptr_offset(leaf, i);
  956. item_len = btrfs_item_size_nr(leaf, i);
  957. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  958. item_len = 0;
  959. if (sizeof(sh) + item_len + *sk_offset >
  960. BTRFS_SEARCH_ARGS_BUFSIZE) {
  961. ret = 1;
  962. goto overflow;
  963. }
  964. btrfs_item_key_to_cpu(leaf, key, i);
  965. if (!key_in_sk(key, sk))
  966. continue;
  967. sh.objectid = key->objectid;
  968. sh.offset = key->offset;
  969. sh.type = key->type;
  970. sh.len = item_len;
  971. sh.transid = found_transid;
  972. /* copy search result header */
  973. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  974. *sk_offset += sizeof(sh);
  975. if (item_len) {
  976. char *p = buf + *sk_offset;
  977. /* copy the item */
  978. read_extent_buffer(leaf, p,
  979. item_off, item_len);
  980. *sk_offset += item_len;
  981. }
  982. found++;
  983. if (*num_found >= sk->nr_items)
  984. break;
  985. }
  986. advance_key:
  987. ret = 0;
  988. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  989. key->offset++;
  990. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  991. key->offset = 0;
  992. key->type++;
  993. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  994. key->offset = 0;
  995. key->type = 0;
  996. key->objectid++;
  997. } else
  998. ret = 1;
  999. overflow:
  1000. *num_found += found;
  1001. return ret;
  1002. }
  1003. static noinline int search_ioctl(struct inode *inode,
  1004. struct btrfs_ioctl_search_args *args)
  1005. {
  1006. struct btrfs_root *root;
  1007. struct btrfs_key key;
  1008. struct btrfs_key max_key;
  1009. struct btrfs_path *path;
  1010. struct btrfs_ioctl_search_key *sk = &args->key;
  1011. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1012. int ret;
  1013. int num_found = 0;
  1014. unsigned long sk_offset = 0;
  1015. path = btrfs_alloc_path();
  1016. if (!path)
  1017. return -ENOMEM;
  1018. if (sk->tree_id == 0) {
  1019. /* search the root of the inode that was passed */
  1020. root = BTRFS_I(inode)->root;
  1021. } else {
  1022. key.objectid = sk->tree_id;
  1023. key.type = BTRFS_ROOT_ITEM_KEY;
  1024. key.offset = (u64)-1;
  1025. root = btrfs_read_fs_root_no_name(info, &key);
  1026. if (IS_ERR(root)) {
  1027. printk(KERN_ERR "could not find root %llu\n",
  1028. sk->tree_id);
  1029. btrfs_free_path(path);
  1030. return -ENOENT;
  1031. }
  1032. }
  1033. key.objectid = sk->min_objectid;
  1034. key.type = sk->min_type;
  1035. key.offset = sk->min_offset;
  1036. max_key.objectid = sk->max_objectid;
  1037. max_key.type = sk->max_type;
  1038. max_key.offset = sk->max_offset;
  1039. path->keep_locks = 1;
  1040. while(1) {
  1041. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1042. sk->min_transid);
  1043. if (ret != 0) {
  1044. if (ret > 0)
  1045. ret = 0;
  1046. goto err;
  1047. }
  1048. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1049. &sk_offset, &num_found);
  1050. btrfs_release_path(root, path);
  1051. if (ret || num_found >= sk->nr_items)
  1052. break;
  1053. }
  1054. ret = 0;
  1055. err:
  1056. sk->nr_items = num_found;
  1057. btrfs_free_path(path);
  1058. return ret;
  1059. }
  1060. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1061. void __user *argp)
  1062. {
  1063. struct btrfs_ioctl_search_args *args;
  1064. struct inode *inode;
  1065. int ret;
  1066. if (!capable(CAP_SYS_ADMIN))
  1067. return -EPERM;
  1068. args = memdup_user(argp, sizeof(*args));
  1069. if (IS_ERR(args))
  1070. return PTR_ERR(args);
  1071. inode = fdentry(file)->d_inode;
  1072. ret = search_ioctl(inode, args);
  1073. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1074. ret = -EFAULT;
  1075. kfree(args);
  1076. return ret;
  1077. }
  1078. /*
  1079. * Search INODE_REFs to identify path name of 'dirid' directory
  1080. * in a 'tree_id' tree. and sets path name to 'name'.
  1081. */
  1082. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1083. u64 tree_id, u64 dirid, char *name)
  1084. {
  1085. struct btrfs_root *root;
  1086. struct btrfs_key key;
  1087. char *ptr;
  1088. int ret = -1;
  1089. int slot;
  1090. int len;
  1091. int total_len = 0;
  1092. struct btrfs_inode_ref *iref;
  1093. struct extent_buffer *l;
  1094. struct btrfs_path *path;
  1095. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1096. name[0]='\0';
  1097. return 0;
  1098. }
  1099. path = btrfs_alloc_path();
  1100. if (!path)
  1101. return -ENOMEM;
  1102. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1103. key.objectid = tree_id;
  1104. key.type = BTRFS_ROOT_ITEM_KEY;
  1105. key.offset = (u64)-1;
  1106. root = btrfs_read_fs_root_no_name(info, &key);
  1107. if (IS_ERR(root)) {
  1108. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1109. ret = -ENOENT;
  1110. goto out;
  1111. }
  1112. key.objectid = dirid;
  1113. key.type = BTRFS_INODE_REF_KEY;
  1114. key.offset = (u64)-1;
  1115. while(1) {
  1116. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1117. if (ret < 0)
  1118. goto out;
  1119. l = path->nodes[0];
  1120. slot = path->slots[0];
  1121. if (ret > 0 && slot > 0)
  1122. slot--;
  1123. btrfs_item_key_to_cpu(l, &key, slot);
  1124. if (ret > 0 && (key.objectid != dirid ||
  1125. key.type != BTRFS_INODE_REF_KEY)) {
  1126. ret = -ENOENT;
  1127. goto out;
  1128. }
  1129. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1130. len = btrfs_inode_ref_name_len(l, iref);
  1131. ptr -= len + 1;
  1132. total_len += len + 1;
  1133. if (ptr < name)
  1134. goto out;
  1135. *(ptr + len) = '/';
  1136. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1137. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1138. break;
  1139. btrfs_release_path(root, path);
  1140. key.objectid = key.offset;
  1141. key.offset = (u64)-1;
  1142. dirid = key.objectid;
  1143. }
  1144. if (ptr < name)
  1145. goto out;
  1146. memcpy(name, ptr, total_len);
  1147. name[total_len]='\0';
  1148. ret = 0;
  1149. out:
  1150. btrfs_free_path(path);
  1151. return ret;
  1152. }
  1153. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1154. void __user *argp)
  1155. {
  1156. struct btrfs_ioctl_ino_lookup_args *args;
  1157. struct inode *inode;
  1158. int ret;
  1159. if (!capable(CAP_SYS_ADMIN))
  1160. return -EPERM;
  1161. args = memdup_user(argp, sizeof(*args));
  1162. if (IS_ERR(args))
  1163. return PTR_ERR(args);
  1164. inode = fdentry(file)->d_inode;
  1165. if (args->treeid == 0)
  1166. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1167. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1168. args->treeid, args->objectid,
  1169. args->name);
  1170. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1171. ret = -EFAULT;
  1172. kfree(args);
  1173. return ret;
  1174. }
  1175. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1176. void __user *arg)
  1177. {
  1178. struct dentry *parent = fdentry(file);
  1179. struct dentry *dentry;
  1180. struct inode *dir = parent->d_inode;
  1181. struct inode *inode;
  1182. struct btrfs_root *root = BTRFS_I(dir)->root;
  1183. struct btrfs_root *dest = NULL;
  1184. struct btrfs_ioctl_vol_args *vol_args;
  1185. struct btrfs_trans_handle *trans;
  1186. int namelen;
  1187. int ret;
  1188. int err = 0;
  1189. vol_args = memdup_user(arg, sizeof(*vol_args));
  1190. if (IS_ERR(vol_args))
  1191. return PTR_ERR(vol_args);
  1192. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1193. namelen = strlen(vol_args->name);
  1194. if (strchr(vol_args->name, '/') ||
  1195. strncmp(vol_args->name, "..", namelen) == 0) {
  1196. err = -EINVAL;
  1197. goto out;
  1198. }
  1199. err = mnt_want_write(file->f_path.mnt);
  1200. if (err)
  1201. goto out;
  1202. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1203. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1204. if (IS_ERR(dentry)) {
  1205. err = PTR_ERR(dentry);
  1206. goto out_unlock_dir;
  1207. }
  1208. if (!dentry->d_inode) {
  1209. err = -ENOENT;
  1210. goto out_dput;
  1211. }
  1212. inode = dentry->d_inode;
  1213. dest = BTRFS_I(inode)->root;
  1214. if (!capable(CAP_SYS_ADMIN)){
  1215. /*
  1216. * Regular user. Only allow this with a special mount
  1217. * option, when the user has write+exec access to the
  1218. * subvol root, and when rmdir(2) would have been
  1219. * allowed.
  1220. *
  1221. * Note that this is _not_ check that the subvol is
  1222. * empty or doesn't contain data that we wouldn't
  1223. * otherwise be able to delete.
  1224. *
  1225. * Users who want to delete empty subvols should try
  1226. * rmdir(2).
  1227. */
  1228. err = -EPERM;
  1229. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1230. goto out_dput;
  1231. /*
  1232. * Do not allow deletion if the parent dir is the same
  1233. * as the dir to be deleted. That means the ioctl
  1234. * must be called on the dentry referencing the root
  1235. * of the subvol, not a random directory contained
  1236. * within it.
  1237. */
  1238. err = -EINVAL;
  1239. if (root == dest)
  1240. goto out_dput;
  1241. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1242. if (err)
  1243. goto out_dput;
  1244. /* check if subvolume may be deleted by a non-root user */
  1245. err = btrfs_may_delete(dir, dentry, 1);
  1246. if (err)
  1247. goto out_dput;
  1248. }
  1249. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1250. err = -EINVAL;
  1251. goto out_dput;
  1252. }
  1253. mutex_lock(&inode->i_mutex);
  1254. err = d_invalidate(dentry);
  1255. if (err)
  1256. goto out_unlock;
  1257. down_write(&root->fs_info->subvol_sem);
  1258. err = may_destroy_subvol(dest);
  1259. if (err)
  1260. goto out_up_write;
  1261. trans = btrfs_start_transaction(root, 0);
  1262. if (IS_ERR(trans)) {
  1263. err = PTR_ERR(trans);
  1264. goto out_up_write;
  1265. }
  1266. trans->block_rsv = &root->fs_info->global_block_rsv;
  1267. ret = btrfs_unlink_subvol(trans, root, dir,
  1268. dest->root_key.objectid,
  1269. dentry->d_name.name,
  1270. dentry->d_name.len);
  1271. BUG_ON(ret);
  1272. btrfs_record_root_in_trans(trans, dest);
  1273. memset(&dest->root_item.drop_progress, 0,
  1274. sizeof(dest->root_item.drop_progress));
  1275. dest->root_item.drop_level = 0;
  1276. btrfs_set_root_refs(&dest->root_item, 0);
  1277. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1278. ret = btrfs_insert_orphan_item(trans,
  1279. root->fs_info->tree_root,
  1280. dest->root_key.objectid);
  1281. BUG_ON(ret);
  1282. }
  1283. ret = btrfs_end_transaction(trans, root);
  1284. BUG_ON(ret);
  1285. inode->i_flags |= S_DEAD;
  1286. out_up_write:
  1287. up_write(&root->fs_info->subvol_sem);
  1288. out_unlock:
  1289. mutex_unlock(&inode->i_mutex);
  1290. if (!err) {
  1291. shrink_dcache_sb(root->fs_info->sb);
  1292. btrfs_invalidate_inodes(dest);
  1293. d_delete(dentry);
  1294. }
  1295. out_dput:
  1296. dput(dentry);
  1297. out_unlock_dir:
  1298. mutex_unlock(&dir->i_mutex);
  1299. mnt_drop_write(file->f_path.mnt);
  1300. out:
  1301. kfree(vol_args);
  1302. return err;
  1303. }
  1304. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1305. {
  1306. struct inode *inode = fdentry(file)->d_inode;
  1307. struct btrfs_root *root = BTRFS_I(inode)->root;
  1308. struct btrfs_ioctl_defrag_range_args *range;
  1309. int ret;
  1310. ret = mnt_want_write(file->f_path.mnt);
  1311. if (ret)
  1312. return ret;
  1313. switch (inode->i_mode & S_IFMT) {
  1314. case S_IFDIR:
  1315. if (!capable(CAP_SYS_ADMIN)) {
  1316. ret = -EPERM;
  1317. goto out;
  1318. }
  1319. ret = btrfs_defrag_root(root, 0);
  1320. if (ret)
  1321. goto out;
  1322. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1323. break;
  1324. case S_IFREG:
  1325. if (!(file->f_mode & FMODE_WRITE)) {
  1326. ret = -EINVAL;
  1327. goto out;
  1328. }
  1329. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1330. if (!range) {
  1331. ret = -ENOMEM;
  1332. goto out;
  1333. }
  1334. if (argp) {
  1335. if (copy_from_user(range, argp,
  1336. sizeof(*range))) {
  1337. ret = -EFAULT;
  1338. kfree(range);
  1339. goto out;
  1340. }
  1341. /* compression requires us to start the IO */
  1342. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1343. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1344. range->extent_thresh = (u32)-1;
  1345. }
  1346. } else {
  1347. /* the rest are all set to zero by kzalloc */
  1348. range->len = (u64)-1;
  1349. }
  1350. ret = btrfs_defrag_file(file, range);
  1351. kfree(range);
  1352. break;
  1353. default:
  1354. ret = -EINVAL;
  1355. }
  1356. out:
  1357. mnt_drop_write(file->f_path.mnt);
  1358. return ret;
  1359. }
  1360. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1361. {
  1362. struct btrfs_ioctl_vol_args *vol_args;
  1363. int ret;
  1364. if (!capable(CAP_SYS_ADMIN))
  1365. return -EPERM;
  1366. vol_args = memdup_user(arg, sizeof(*vol_args));
  1367. if (IS_ERR(vol_args))
  1368. return PTR_ERR(vol_args);
  1369. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1370. ret = btrfs_init_new_device(root, vol_args->name);
  1371. kfree(vol_args);
  1372. return ret;
  1373. }
  1374. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1375. {
  1376. struct btrfs_ioctl_vol_args *vol_args;
  1377. int ret;
  1378. if (!capable(CAP_SYS_ADMIN))
  1379. return -EPERM;
  1380. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1381. return -EROFS;
  1382. vol_args = memdup_user(arg, sizeof(*vol_args));
  1383. if (IS_ERR(vol_args))
  1384. return PTR_ERR(vol_args);
  1385. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1386. ret = btrfs_rm_device(root, vol_args->name);
  1387. kfree(vol_args);
  1388. return ret;
  1389. }
  1390. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1391. u64 off, u64 olen, u64 destoff)
  1392. {
  1393. struct inode *inode = fdentry(file)->d_inode;
  1394. struct btrfs_root *root = BTRFS_I(inode)->root;
  1395. struct file *src_file;
  1396. struct inode *src;
  1397. struct btrfs_trans_handle *trans;
  1398. struct btrfs_path *path;
  1399. struct extent_buffer *leaf;
  1400. char *buf;
  1401. struct btrfs_key key;
  1402. u32 nritems;
  1403. int slot;
  1404. int ret;
  1405. u64 len = olen;
  1406. u64 bs = root->fs_info->sb->s_blocksize;
  1407. u64 hint_byte;
  1408. /*
  1409. * TODO:
  1410. * - split compressed inline extents. annoying: we need to
  1411. * decompress into destination's address_space (the file offset
  1412. * may change, so source mapping won't do), then recompress (or
  1413. * otherwise reinsert) a subrange.
  1414. * - allow ranges within the same file to be cloned (provided
  1415. * they don't overlap)?
  1416. */
  1417. /* the destination must be opened for writing */
  1418. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1419. return -EINVAL;
  1420. ret = mnt_want_write(file->f_path.mnt);
  1421. if (ret)
  1422. return ret;
  1423. src_file = fget(srcfd);
  1424. if (!src_file) {
  1425. ret = -EBADF;
  1426. goto out_drop_write;
  1427. }
  1428. src = src_file->f_dentry->d_inode;
  1429. ret = -EINVAL;
  1430. if (src == inode)
  1431. goto out_fput;
  1432. /* the src must be open for reading */
  1433. if (!(src_file->f_mode & FMODE_READ))
  1434. goto out_fput;
  1435. ret = -EISDIR;
  1436. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1437. goto out_fput;
  1438. ret = -EXDEV;
  1439. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1440. goto out_fput;
  1441. ret = -ENOMEM;
  1442. buf = vmalloc(btrfs_level_size(root, 0));
  1443. if (!buf)
  1444. goto out_fput;
  1445. path = btrfs_alloc_path();
  1446. if (!path) {
  1447. vfree(buf);
  1448. goto out_fput;
  1449. }
  1450. path->reada = 2;
  1451. if (inode < src) {
  1452. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1453. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1454. } else {
  1455. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1456. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1457. }
  1458. /* determine range to clone */
  1459. ret = -EINVAL;
  1460. if (off + len > src->i_size || off + len < off)
  1461. goto out_unlock;
  1462. if (len == 0)
  1463. olen = len = src->i_size - off;
  1464. /* if we extend to eof, continue to block boundary */
  1465. if (off + len == src->i_size)
  1466. len = ALIGN(src->i_size, bs) - off;
  1467. /* verify the end result is block aligned */
  1468. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1469. !IS_ALIGNED(destoff, bs))
  1470. goto out_unlock;
  1471. /* do any pending delalloc/csum calc on src, one way or
  1472. another, and lock file content */
  1473. while (1) {
  1474. struct btrfs_ordered_extent *ordered;
  1475. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1476. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1477. if (!ordered &&
  1478. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1479. EXTENT_DELALLOC, 0, NULL))
  1480. break;
  1481. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1482. if (ordered)
  1483. btrfs_put_ordered_extent(ordered);
  1484. btrfs_wait_ordered_range(src, off, len);
  1485. }
  1486. /* clone data */
  1487. key.objectid = src->i_ino;
  1488. key.type = BTRFS_EXTENT_DATA_KEY;
  1489. key.offset = 0;
  1490. while (1) {
  1491. /*
  1492. * note the key will change type as we walk through the
  1493. * tree.
  1494. */
  1495. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1496. if (ret < 0)
  1497. goto out;
  1498. nritems = btrfs_header_nritems(path->nodes[0]);
  1499. if (path->slots[0] >= nritems) {
  1500. ret = btrfs_next_leaf(root, path);
  1501. if (ret < 0)
  1502. goto out;
  1503. if (ret > 0)
  1504. break;
  1505. nritems = btrfs_header_nritems(path->nodes[0]);
  1506. }
  1507. leaf = path->nodes[0];
  1508. slot = path->slots[0];
  1509. btrfs_item_key_to_cpu(leaf, &key, slot);
  1510. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1511. key.objectid != src->i_ino)
  1512. break;
  1513. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1514. struct btrfs_file_extent_item *extent;
  1515. int type;
  1516. u32 size;
  1517. struct btrfs_key new_key;
  1518. u64 disko = 0, diskl = 0;
  1519. u64 datao = 0, datal = 0;
  1520. u8 comp;
  1521. u64 endoff;
  1522. size = btrfs_item_size_nr(leaf, slot);
  1523. read_extent_buffer(leaf, buf,
  1524. btrfs_item_ptr_offset(leaf, slot),
  1525. size);
  1526. extent = btrfs_item_ptr(leaf, slot,
  1527. struct btrfs_file_extent_item);
  1528. comp = btrfs_file_extent_compression(leaf, extent);
  1529. type = btrfs_file_extent_type(leaf, extent);
  1530. if (type == BTRFS_FILE_EXTENT_REG ||
  1531. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1532. disko = btrfs_file_extent_disk_bytenr(leaf,
  1533. extent);
  1534. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1535. extent);
  1536. datao = btrfs_file_extent_offset(leaf, extent);
  1537. datal = btrfs_file_extent_num_bytes(leaf,
  1538. extent);
  1539. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1540. /* take upper bound, may be compressed */
  1541. datal = btrfs_file_extent_ram_bytes(leaf,
  1542. extent);
  1543. }
  1544. btrfs_release_path(root, path);
  1545. if (key.offset + datal <= off ||
  1546. key.offset >= off+len)
  1547. goto next;
  1548. memcpy(&new_key, &key, sizeof(new_key));
  1549. new_key.objectid = inode->i_ino;
  1550. new_key.offset = key.offset + destoff - off;
  1551. trans = btrfs_start_transaction(root, 1);
  1552. if (IS_ERR(trans)) {
  1553. ret = PTR_ERR(trans);
  1554. goto out;
  1555. }
  1556. if (type == BTRFS_FILE_EXTENT_REG ||
  1557. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1558. if (off > key.offset) {
  1559. datao += off - key.offset;
  1560. datal -= off - key.offset;
  1561. }
  1562. if (key.offset + datal > off + len)
  1563. datal = off + len - key.offset;
  1564. ret = btrfs_drop_extents(trans, inode,
  1565. new_key.offset,
  1566. new_key.offset + datal,
  1567. &hint_byte, 1);
  1568. BUG_ON(ret);
  1569. ret = btrfs_insert_empty_item(trans, root, path,
  1570. &new_key, size);
  1571. BUG_ON(ret);
  1572. leaf = path->nodes[0];
  1573. slot = path->slots[0];
  1574. write_extent_buffer(leaf, buf,
  1575. btrfs_item_ptr_offset(leaf, slot),
  1576. size);
  1577. extent = btrfs_item_ptr(leaf, slot,
  1578. struct btrfs_file_extent_item);
  1579. /* disko == 0 means it's a hole */
  1580. if (!disko)
  1581. datao = 0;
  1582. btrfs_set_file_extent_offset(leaf, extent,
  1583. datao);
  1584. btrfs_set_file_extent_num_bytes(leaf, extent,
  1585. datal);
  1586. if (disko) {
  1587. inode_add_bytes(inode, datal);
  1588. ret = btrfs_inc_extent_ref(trans, root,
  1589. disko, diskl, 0,
  1590. root->root_key.objectid,
  1591. inode->i_ino,
  1592. new_key.offset - datao);
  1593. BUG_ON(ret);
  1594. }
  1595. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1596. u64 skip = 0;
  1597. u64 trim = 0;
  1598. if (off > key.offset) {
  1599. skip = off - key.offset;
  1600. new_key.offset += skip;
  1601. }
  1602. if (key.offset + datal > off+len)
  1603. trim = key.offset + datal - (off+len);
  1604. if (comp && (skip || trim)) {
  1605. ret = -EINVAL;
  1606. btrfs_end_transaction(trans, root);
  1607. goto out;
  1608. }
  1609. size -= skip + trim;
  1610. datal -= skip + trim;
  1611. ret = btrfs_drop_extents(trans, inode,
  1612. new_key.offset,
  1613. new_key.offset + datal,
  1614. &hint_byte, 1);
  1615. BUG_ON(ret);
  1616. ret = btrfs_insert_empty_item(trans, root, path,
  1617. &new_key, size);
  1618. BUG_ON(ret);
  1619. if (skip) {
  1620. u32 start =
  1621. btrfs_file_extent_calc_inline_size(0);
  1622. memmove(buf+start, buf+start+skip,
  1623. datal);
  1624. }
  1625. leaf = path->nodes[0];
  1626. slot = path->slots[0];
  1627. write_extent_buffer(leaf, buf,
  1628. btrfs_item_ptr_offset(leaf, slot),
  1629. size);
  1630. inode_add_bytes(inode, datal);
  1631. }
  1632. btrfs_mark_buffer_dirty(leaf);
  1633. btrfs_release_path(root, path);
  1634. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1635. /*
  1636. * we round up to the block size at eof when
  1637. * determining which extents to clone above,
  1638. * but shouldn't round up the file size
  1639. */
  1640. endoff = new_key.offset + datal;
  1641. if (endoff > destoff+olen)
  1642. endoff = destoff+olen;
  1643. if (endoff > inode->i_size)
  1644. btrfs_i_size_write(inode, endoff);
  1645. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1646. ret = btrfs_update_inode(trans, root, inode);
  1647. BUG_ON(ret);
  1648. btrfs_end_transaction(trans, root);
  1649. }
  1650. next:
  1651. btrfs_release_path(root, path);
  1652. key.offset++;
  1653. }
  1654. ret = 0;
  1655. out:
  1656. btrfs_release_path(root, path);
  1657. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1658. out_unlock:
  1659. mutex_unlock(&src->i_mutex);
  1660. mutex_unlock(&inode->i_mutex);
  1661. vfree(buf);
  1662. btrfs_free_path(path);
  1663. out_fput:
  1664. fput(src_file);
  1665. out_drop_write:
  1666. mnt_drop_write(file->f_path.mnt);
  1667. return ret;
  1668. }
  1669. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1670. {
  1671. struct btrfs_ioctl_clone_range_args args;
  1672. if (copy_from_user(&args, argp, sizeof(args)))
  1673. return -EFAULT;
  1674. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1675. args.src_length, args.dest_offset);
  1676. }
  1677. /*
  1678. * there are many ways the trans_start and trans_end ioctls can lead
  1679. * to deadlocks. They should only be used by applications that
  1680. * basically own the machine, and have a very in depth understanding
  1681. * of all the possible deadlocks and enospc problems.
  1682. */
  1683. static long btrfs_ioctl_trans_start(struct file *file)
  1684. {
  1685. struct inode *inode = fdentry(file)->d_inode;
  1686. struct btrfs_root *root = BTRFS_I(inode)->root;
  1687. struct btrfs_trans_handle *trans;
  1688. int ret;
  1689. ret = -EPERM;
  1690. if (!capable(CAP_SYS_ADMIN))
  1691. goto out;
  1692. ret = -EINPROGRESS;
  1693. if (file->private_data)
  1694. goto out;
  1695. ret = mnt_want_write(file->f_path.mnt);
  1696. if (ret)
  1697. goto out;
  1698. mutex_lock(&root->fs_info->trans_mutex);
  1699. root->fs_info->open_ioctl_trans++;
  1700. mutex_unlock(&root->fs_info->trans_mutex);
  1701. ret = -ENOMEM;
  1702. trans = btrfs_start_ioctl_transaction(root, 0);
  1703. if (!trans)
  1704. goto out_drop;
  1705. file->private_data = trans;
  1706. return 0;
  1707. out_drop:
  1708. mutex_lock(&root->fs_info->trans_mutex);
  1709. root->fs_info->open_ioctl_trans--;
  1710. mutex_unlock(&root->fs_info->trans_mutex);
  1711. mnt_drop_write(file->f_path.mnt);
  1712. out:
  1713. return ret;
  1714. }
  1715. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1716. {
  1717. struct inode *inode = fdentry(file)->d_inode;
  1718. struct btrfs_root *root = BTRFS_I(inode)->root;
  1719. struct btrfs_root *new_root;
  1720. struct btrfs_dir_item *di;
  1721. struct btrfs_trans_handle *trans;
  1722. struct btrfs_path *path;
  1723. struct btrfs_key location;
  1724. struct btrfs_disk_key disk_key;
  1725. struct btrfs_super_block *disk_super;
  1726. u64 features;
  1727. u64 objectid = 0;
  1728. u64 dir_id;
  1729. if (!capable(CAP_SYS_ADMIN))
  1730. return -EPERM;
  1731. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1732. return -EFAULT;
  1733. if (!objectid)
  1734. objectid = root->root_key.objectid;
  1735. location.objectid = objectid;
  1736. location.type = BTRFS_ROOT_ITEM_KEY;
  1737. location.offset = (u64)-1;
  1738. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1739. if (IS_ERR(new_root))
  1740. return PTR_ERR(new_root);
  1741. if (btrfs_root_refs(&new_root->root_item) == 0)
  1742. return -ENOENT;
  1743. path = btrfs_alloc_path();
  1744. if (!path)
  1745. return -ENOMEM;
  1746. path->leave_spinning = 1;
  1747. trans = btrfs_start_transaction(root, 1);
  1748. if (!trans) {
  1749. btrfs_free_path(path);
  1750. return -ENOMEM;
  1751. }
  1752. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1753. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1754. dir_id, "default", 7, 1);
  1755. if (IS_ERR_OR_NULL(di)) {
  1756. btrfs_free_path(path);
  1757. btrfs_end_transaction(trans, root);
  1758. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1759. "this isn't going to work\n");
  1760. return -ENOENT;
  1761. }
  1762. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1763. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1764. btrfs_mark_buffer_dirty(path->nodes[0]);
  1765. btrfs_free_path(path);
  1766. disk_super = &root->fs_info->super_copy;
  1767. features = btrfs_super_incompat_flags(disk_super);
  1768. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1769. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1770. btrfs_set_super_incompat_flags(disk_super, features);
  1771. }
  1772. btrfs_end_transaction(trans, root);
  1773. return 0;
  1774. }
  1775. static void get_block_group_info(struct list_head *groups_list,
  1776. struct btrfs_ioctl_space_info *space)
  1777. {
  1778. struct btrfs_block_group_cache *block_group;
  1779. space->total_bytes = 0;
  1780. space->used_bytes = 0;
  1781. space->flags = 0;
  1782. list_for_each_entry(block_group, groups_list, list) {
  1783. space->flags = block_group->flags;
  1784. space->total_bytes += block_group->key.offset;
  1785. space->used_bytes +=
  1786. btrfs_block_group_used(&block_group->item);
  1787. }
  1788. }
  1789. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1790. {
  1791. struct btrfs_ioctl_space_args space_args;
  1792. struct btrfs_ioctl_space_info space;
  1793. struct btrfs_ioctl_space_info *dest;
  1794. struct btrfs_ioctl_space_info *dest_orig;
  1795. struct btrfs_ioctl_space_info *user_dest;
  1796. struct btrfs_space_info *info;
  1797. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1798. BTRFS_BLOCK_GROUP_SYSTEM,
  1799. BTRFS_BLOCK_GROUP_METADATA,
  1800. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1801. int num_types = 4;
  1802. int alloc_size;
  1803. int ret = 0;
  1804. int slot_count = 0;
  1805. int i, c;
  1806. if (copy_from_user(&space_args,
  1807. (struct btrfs_ioctl_space_args __user *)arg,
  1808. sizeof(space_args)))
  1809. return -EFAULT;
  1810. for (i = 0; i < num_types; i++) {
  1811. struct btrfs_space_info *tmp;
  1812. info = NULL;
  1813. rcu_read_lock();
  1814. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1815. list) {
  1816. if (tmp->flags == types[i]) {
  1817. info = tmp;
  1818. break;
  1819. }
  1820. }
  1821. rcu_read_unlock();
  1822. if (!info)
  1823. continue;
  1824. down_read(&info->groups_sem);
  1825. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1826. if (!list_empty(&info->block_groups[c]))
  1827. slot_count++;
  1828. }
  1829. up_read(&info->groups_sem);
  1830. }
  1831. /* space_slots == 0 means they are asking for a count */
  1832. if (space_args.space_slots == 0) {
  1833. space_args.total_spaces = slot_count;
  1834. goto out;
  1835. }
  1836. slot_count = min_t(int, space_args.space_slots, slot_count);
  1837. alloc_size = sizeof(*dest) * slot_count;
  1838. /* we generally have at most 6 or so space infos, one for each raid
  1839. * level. So, a whole page should be more than enough for everyone
  1840. */
  1841. if (alloc_size > PAGE_CACHE_SIZE)
  1842. return -ENOMEM;
  1843. space_args.total_spaces = 0;
  1844. dest = kmalloc(alloc_size, GFP_NOFS);
  1845. if (!dest)
  1846. return -ENOMEM;
  1847. dest_orig = dest;
  1848. /* now we have a buffer to copy into */
  1849. for (i = 0; i < num_types; i++) {
  1850. struct btrfs_space_info *tmp;
  1851. info = NULL;
  1852. rcu_read_lock();
  1853. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1854. list) {
  1855. if (tmp->flags == types[i]) {
  1856. info = tmp;
  1857. break;
  1858. }
  1859. }
  1860. rcu_read_unlock();
  1861. if (!info)
  1862. continue;
  1863. down_read(&info->groups_sem);
  1864. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1865. if (!list_empty(&info->block_groups[c])) {
  1866. get_block_group_info(&info->block_groups[c],
  1867. &space);
  1868. memcpy(dest, &space, sizeof(space));
  1869. dest++;
  1870. space_args.total_spaces++;
  1871. }
  1872. }
  1873. up_read(&info->groups_sem);
  1874. }
  1875. user_dest = (struct btrfs_ioctl_space_info *)
  1876. (arg + sizeof(struct btrfs_ioctl_space_args));
  1877. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1878. ret = -EFAULT;
  1879. kfree(dest_orig);
  1880. out:
  1881. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1882. ret = -EFAULT;
  1883. return ret;
  1884. }
  1885. /*
  1886. * there are many ways the trans_start and trans_end ioctls can lead
  1887. * to deadlocks. They should only be used by applications that
  1888. * basically own the machine, and have a very in depth understanding
  1889. * of all the possible deadlocks and enospc problems.
  1890. */
  1891. long btrfs_ioctl_trans_end(struct file *file)
  1892. {
  1893. struct inode *inode = fdentry(file)->d_inode;
  1894. struct btrfs_root *root = BTRFS_I(inode)->root;
  1895. struct btrfs_trans_handle *trans;
  1896. trans = file->private_data;
  1897. if (!trans)
  1898. return -EINVAL;
  1899. file->private_data = NULL;
  1900. btrfs_end_transaction(trans, root);
  1901. mutex_lock(&root->fs_info->trans_mutex);
  1902. root->fs_info->open_ioctl_trans--;
  1903. mutex_unlock(&root->fs_info->trans_mutex);
  1904. mnt_drop_write(file->f_path.mnt);
  1905. return 0;
  1906. }
  1907. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1908. {
  1909. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1910. struct btrfs_trans_handle *trans;
  1911. u64 transid;
  1912. trans = btrfs_start_transaction(root, 0);
  1913. transid = trans->transid;
  1914. btrfs_commit_transaction_async(trans, root, 0);
  1915. if (argp)
  1916. if (copy_to_user(argp, &transid, sizeof(transid)))
  1917. return -EFAULT;
  1918. return 0;
  1919. }
  1920. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1921. {
  1922. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1923. u64 transid;
  1924. if (argp) {
  1925. if (copy_from_user(&transid, argp, sizeof(transid)))
  1926. return -EFAULT;
  1927. } else {
  1928. transid = 0; /* current trans */
  1929. }
  1930. return btrfs_wait_for_commit(root, transid);
  1931. }
  1932. long btrfs_ioctl(struct file *file, unsigned int
  1933. cmd, unsigned long arg)
  1934. {
  1935. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1936. void __user *argp = (void __user *)arg;
  1937. switch (cmd) {
  1938. case FS_IOC_GETFLAGS:
  1939. return btrfs_ioctl_getflags(file, argp);
  1940. case FS_IOC_SETFLAGS:
  1941. return btrfs_ioctl_setflags(file, argp);
  1942. case FS_IOC_GETVERSION:
  1943. return btrfs_ioctl_getversion(file, argp);
  1944. case BTRFS_IOC_SNAP_CREATE:
  1945. return btrfs_ioctl_snap_create(file, argp, 0, 0);
  1946. case BTRFS_IOC_SNAP_CREATE_V2:
  1947. return btrfs_ioctl_snap_create(file, argp, 0, 1);
  1948. case BTRFS_IOC_SUBVOL_CREATE:
  1949. return btrfs_ioctl_snap_create(file, argp, 1, 0);
  1950. case BTRFS_IOC_SNAP_DESTROY:
  1951. return btrfs_ioctl_snap_destroy(file, argp);
  1952. case BTRFS_IOC_DEFAULT_SUBVOL:
  1953. return btrfs_ioctl_default_subvol(file, argp);
  1954. case BTRFS_IOC_DEFRAG:
  1955. return btrfs_ioctl_defrag(file, NULL);
  1956. case BTRFS_IOC_DEFRAG_RANGE:
  1957. return btrfs_ioctl_defrag(file, argp);
  1958. case BTRFS_IOC_RESIZE:
  1959. return btrfs_ioctl_resize(root, argp);
  1960. case BTRFS_IOC_ADD_DEV:
  1961. return btrfs_ioctl_add_dev(root, argp);
  1962. case BTRFS_IOC_RM_DEV:
  1963. return btrfs_ioctl_rm_dev(root, argp);
  1964. case BTRFS_IOC_BALANCE:
  1965. return btrfs_balance(root->fs_info->dev_root);
  1966. case BTRFS_IOC_CLONE:
  1967. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1968. case BTRFS_IOC_CLONE_RANGE:
  1969. return btrfs_ioctl_clone_range(file, argp);
  1970. case BTRFS_IOC_TRANS_START:
  1971. return btrfs_ioctl_trans_start(file);
  1972. case BTRFS_IOC_TRANS_END:
  1973. return btrfs_ioctl_trans_end(file);
  1974. case BTRFS_IOC_TREE_SEARCH:
  1975. return btrfs_ioctl_tree_search(file, argp);
  1976. case BTRFS_IOC_INO_LOOKUP:
  1977. return btrfs_ioctl_ino_lookup(file, argp);
  1978. case BTRFS_IOC_SPACE_INFO:
  1979. return btrfs_ioctl_space_info(root, argp);
  1980. case BTRFS_IOC_SYNC:
  1981. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1982. return 0;
  1983. case BTRFS_IOC_START_SYNC:
  1984. return btrfs_ioctl_start_sync(file, argp);
  1985. case BTRFS_IOC_WAIT_SYNC:
  1986. return btrfs_ioctl_wait_sync(file, argp);
  1987. }
  1988. return -ENOTTY;
  1989. }