ops_inode.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/swap.h>
  21. #include <linux/falloc.h>
  22. #include <asm/uaccess.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "xattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. /**
  38. * gfs2_create - Create a file
  39. * @dir: The directory in which to create the file
  40. * @dentry: The dentry of the new file
  41. * @mode: The mode of the new file
  42. *
  43. * Returns: errno
  44. */
  45. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  46. int mode, struct nameidata *nd)
  47. {
  48. struct gfs2_inode *dip = GFS2_I(dir);
  49. struct gfs2_sbd *sdp = GFS2_SB(dir);
  50. struct gfs2_holder ghs[2];
  51. struct inode *inode;
  52. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  53. for (;;) {
  54. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  55. if (!IS_ERR(inode)) {
  56. gfs2_trans_end(sdp);
  57. if (dip->i_alloc->al_rgd)
  58. gfs2_inplace_release(dip);
  59. gfs2_quota_unlock(dip);
  60. gfs2_alloc_put(dip);
  61. gfs2_glock_dq_uninit_m(2, ghs);
  62. mark_inode_dirty(inode);
  63. break;
  64. } else if (PTR_ERR(inode) != -EEXIST ||
  65. (nd && nd->flags & LOOKUP_EXCL)) {
  66. gfs2_holder_uninit(ghs);
  67. return PTR_ERR(inode);
  68. }
  69. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  70. if (inode) {
  71. if (!IS_ERR(inode)) {
  72. gfs2_holder_uninit(ghs);
  73. break;
  74. } else {
  75. gfs2_holder_uninit(ghs);
  76. return PTR_ERR(inode);
  77. }
  78. }
  79. }
  80. d_instantiate(dentry, inode);
  81. return 0;
  82. }
  83. /**
  84. * gfs2_lookup - Look up a filename in a directory and return its inode
  85. * @dir: The directory inode
  86. * @dentry: The dentry of the new inode
  87. * @nd: passed from Linux VFS, ignored by us
  88. *
  89. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  90. *
  91. * Returns: errno
  92. */
  93. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  94. struct nameidata *nd)
  95. {
  96. struct inode *inode = NULL;
  97. dentry->d_op = &gfs2_dops;
  98. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  99. if (inode && IS_ERR(inode))
  100. return ERR_CAST(inode);
  101. if (inode) {
  102. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  103. struct gfs2_holder gh;
  104. int error;
  105. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  106. if (error) {
  107. iput(inode);
  108. return ERR_PTR(error);
  109. }
  110. gfs2_glock_dq_uninit(&gh);
  111. return d_splice_alias(inode, dentry);
  112. }
  113. d_add(dentry, inode);
  114. return NULL;
  115. }
  116. /**
  117. * gfs2_link - Link to a file
  118. * @old_dentry: The inode to link
  119. * @dir: Add link to this directory
  120. * @dentry: The name of the link
  121. *
  122. * Link the inode in "old_dentry" into the directory "dir" with the
  123. * name in "dentry".
  124. *
  125. * Returns: errno
  126. */
  127. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  128. struct dentry *dentry)
  129. {
  130. struct gfs2_inode *dip = GFS2_I(dir);
  131. struct gfs2_sbd *sdp = GFS2_SB(dir);
  132. struct inode *inode = old_dentry->d_inode;
  133. struct gfs2_inode *ip = GFS2_I(inode);
  134. struct gfs2_holder ghs[2];
  135. int alloc_required;
  136. int error;
  137. if (S_ISDIR(inode->i_mode))
  138. return -EPERM;
  139. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  140. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  141. error = gfs2_glock_nq(ghs); /* parent */
  142. if (error)
  143. goto out_parent;
  144. error = gfs2_glock_nq(ghs + 1); /* child */
  145. if (error)
  146. goto out_child;
  147. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  148. if (error)
  149. goto out_gunlock;
  150. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  151. switch (error) {
  152. case -ENOENT:
  153. break;
  154. case 0:
  155. error = -EEXIST;
  156. default:
  157. goto out_gunlock;
  158. }
  159. error = -EINVAL;
  160. if (!dip->i_inode.i_nlink)
  161. goto out_gunlock;
  162. error = -EFBIG;
  163. if (dip->i_entries == (u32)-1)
  164. goto out_gunlock;
  165. error = -EPERM;
  166. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  167. goto out_gunlock;
  168. error = -EINVAL;
  169. if (!ip->i_inode.i_nlink)
  170. goto out_gunlock;
  171. error = -EMLINK;
  172. if (ip->i_inode.i_nlink == (u32)-1)
  173. goto out_gunlock;
  174. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  175. if (error < 0)
  176. goto out_gunlock;
  177. error = 0;
  178. if (alloc_required) {
  179. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  180. if (!al) {
  181. error = -ENOMEM;
  182. goto out_gunlock;
  183. }
  184. error = gfs2_quota_lock_check(dip);
  185. if (error)
  186. goto out_alloc;
  187. al->al_requested = sdp->sd_max_dirres;
  188. error = gfs2_inplace_reserve(dip);
  189. if (error)
  190. goto out_gunlock_q;
  191. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  192. gfs2_rg_blocks(al) +
  193. 2 * RES_DINODE + RES_STATFS +
  194. RES_QUOTA, 0);
  195. if (error)
  196. goto out_ipres;
  197. } else {
  198. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  199. if (error)
  200. goto out_ipres;
  201. }
  202. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  203. if (error)
  204. goto out_end_trans;
  205. error = gfs2_change_nlink(ip, +1);
  206. out_end_trans:
  207. gfs2_trans_end(sdp);
  208. out_ipres:
  209. if (alloc_required)
  210. gfs2_inplace_release(dip);
  211. out_gunlock_q:
  212. if (alloc_required)
  213. gfs2_quota_unlock(dip);
  214. out_alloc:
  215. if (alloc_required)
  216. gfs2_alloc_put(dip);
  217. out_gunlock:
  218. gfs2_glock_dq(ghs + 1);
  219. out_child:
  220. gfs2_glock_dq(ghs);
  221. out_parent:
  222. gfs2_holder_uninit(ghs);
  223. gfs2_holder_uninit(ghs + 1);
  224. if (!error) {
  225. ihold(inode);
  226. d_instantiate(dentry, inode);
  227. mark_inode_dirty(inode);
  228. }
  229. return error;
  230. }
  231. /*
  232. * gfs2_unlink_ok - check to see that a inode is still in a directory
  233. * @dip: the directory
  234. * @name: the name of the file
  235. * @ip: the inode
  236. *
  237. * Assumes that the lock on (at least) @dip is held.
  238. *
  239. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  240. */
  241. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  242. const struct gfs2_inode *ip)
  243. {
  244. int error;
  245. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  246. return -EPERM;
  247. if ((dip->i_inode.i_mode & S_ISVTX) &&
  248. dip->i_inode.i_uid != current_fsuid() &&
  249. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  250. return -EPERM;
  251. if (IS_APPEND(&dip->i_inode))
  252. return -EPERM;
  253. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  254. if (error)
  255. return error;
  256. error = gfs2_dir_check(&dip->i_inode, name, ip);
  257. if (error)
  258. return error;
  259. return 0;
  260. }
  261. /**
  262. * gfs2_unlink - Unlink a file
  263. * @dir: The inode of the directory containing the file to unlink
  264. * @dentry: The file itself
  265. *
  266. * Unlink a file. Call gfs2_unlinki()
  267. *
  268. * Returns: errno
  269. */
  270. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  271. {
  272. struct gfs2_inode *dip = GFS2_I(dir);
  273. struct gfs2_sbd *sdp = GFS2_SB(dir);
  274. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  275. struct gfs2_holder ghs[3];
  276. struct gfs2_rgrpd *rgd;
  277. struct gfs2_holder ri_gh;
  278. int error;
  279. error = gfs2_rindex_hold(sdp, &ri_gh);
  280. if (error)
  281. return error;
  282. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  283. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  284. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  285. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  286. error = gfs2_glock_nq(ghs); /* parent */
  287. if (error)
  288. goto out_parent;
  289. error = gfs2_glock_nq(ghs + 1); /* child */
  290. if (error)
  291. goto out_child;
  292. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  293. if (error)
  294. goto out_rgrp;
  295. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  296. if (error)
  297. goto out_gunlock;
  298. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  299. if (error)
  300. goto out_gunlock;
  301. error = gfs2_dir_del(dip, &dentry->d_name);
  302. if (error)
  303. goto out_end_trans;
  304. error = gfs2_change_nlink(ip, -1);
  305. out_end_trans:
  306. gfs2_trans_end(sdp);
  307. out_gunlock:
  308. gfs2_glock_dq(ghs + 2);
  309. out_rgrp:
  310. gfs2_holder_uninit(ghs + 2);
  311. gfs2_glock_dq(ghs + 1);
  312. out_child:
  313. gfs2_holder_uninit(ghs + 1);
  314. gfs2_glock_dq(ghs);
  315. out_parent:
  316. gfs2_holder_uninit(ghs);
  317. gfs2_glock_dq_uninit(&ri_gh);
  318. return error;
  319. }
  320. /**
  321. * gfs2_symlink - Create a symlink
  322. * @dir: The directory to create the symlink in
  323. * @dentry: The dentry to put the symlink in
  324. * @symname: The thing which the link points to
  325. *
  326. * Returns: errno
  327. */
  328. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  329. const char *symname)
  330. {
  331. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  332. struct gfs2_sbd *sdp = GFS2_SB(dir);
  333. struct gfs2_holder ghs[2];
  334. struct inode *inode;
  335. struct buffer_head *dibh;
  336. int size;
  337. int error;
  338. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  339. size = strlen(symname);
  340. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  341. return -ENAMETOOLONG;
  342. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  343. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  344. if (IS_ERR(inode)) {
  345. gfs2_holder_uninit(ghs);
  346. return PTR_ERR(inode);
  347. }
  348. ip = ghs[1].gh_gl->gl_object;
  349. i_size_write(inode, size);
  350. error = gfs2_meta_inode_buffer(ip, &dibh);
  351. if (!gfs2_assert_withdraw(sdp, !error)) {
  352. gfs2_dinode_out(ip, dibh->b_data);
  353. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  354. size);
  355. brelse(dibh);
  356. }
  357. gfs2_trans_end(sdp);
  358. if (dip->i_alloc->al_rgd)
  359. gfs2_inplace_release(dip);
  360. gfs2_quota_unlock(dip);
  361. gfs2_alloc_put(dip);
  362. gfs2_glock_dq_uninit_m(2, ghs);
  363. d_instantiate(dentry, inode);
  364. mark_inode_dirty(inode);
  365. return 0;
  366. }
  367. /**
  368. * gfs2_mkdir - Make a directory
  369. * @dir: The parent directory of the new one
  370. * @dentry: The dentry of the new directory
  371. * @mode: The mode of the new directory
  372. *
  373. * Returns: errno
  374. */
  375. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  376. {
  377. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  378. struct gfs2_sbd *sdp = GFS2_SB(dir);
  379. struct gfs2_holder ghs[2];
  380. struct inode *inode;
  381. struct buffer_head *dibh;
  382. int error;
  383. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  384. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  385. if (IS_ERR(inode)) {
  386. gfs2_holder_uninit(ghs);
  387. return PTR_ERR(inode);
  388. }
  389. ip = ghs[1].gh_gl->gl_object;
  390. ip->i_inode.i_nlink = 2;
  391. i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  392. ip->i_diskflags |= GFS2_DIF_JDATA;
  393. ip->i_entries = 2;
  394. error = gfs2_meta_inode_buffer(ip, &dibh);
  395. if (!gfs2_assert_withdraw(sdp, !error)) {
  396. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  397. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  398. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  399. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  400. dent->de_inum = di->di_num; /* already GFS2 endian */
  401. dent->de_type = cpu_to_be16(DT_DIR);
  402. di->di_entries = cpu_to_be32(1);
  403. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  404. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  405. gfs2_inum_out(dip, dent);
  406. dent->de_type = cpu_to_be16(DT_DIR);
  407. gfs2_dinode_out(ip, di);
  408. brelse(dibh);
  409. }
  410. error = gfs2_change_nlink(dip, +1);
  411. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  412. gfs2_trans_end(sdp);
  413. if (dip->i_alloc->al_rgd)
  414. gfs2_inplace_release(dip);
  415. gfs2_quota_unlock(dip);
  416. gfs2_alloc_put(dip);
  417. gfs2_glock_dq_uninit_m(2, ghs);
  418. d_instantiate(dentry, inode);
  419. mark_inode_dirty(inode);
  420. return 0;
  421. }
  422. /**
  423. * gfs2_rmdiri - Remove a directory
  424. * @dip: The parent directory of the directory to be removed
  425. * @name: The name of the directory to be removed
  426. * @ip: The GFS2 inode of the directory to be removed
  427. *
  428. * Assumes Glocks on dip and ip are held
  429. *
  430. * Returns: errno
  431. */
  432. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  433. struct gfs2_inode *ip)
  434. {
  435. int error;
  436. if (ip->i_entries != 2) {
  437. if (gfs2_consist_inode(ip))
  438. gfs2_dinode_print(ip);
  439. return -EIO;
  440. }
  441. error = gfs2_dir_del(dip, name);
  442. if (error)
  443. return error;
  444. error = gfs2_change_nlink(dip, -1);
  445. if (error)
  446. return error;
  447. error = gfs2_dir_del(ip, &gfs2_qdot);
  448. if (error)
  449. return error;
  450. error = gfs2_dir_del(ip, &gfs2_qdotdot);
  451. if (error)
  452. return error;
  453. /* It looks odd, but it really should be done twice */
  454. error = gfs2_change_nlink(ip, -1);
  455. if (error)
  456. return error;
  457. error = gfs2_change_nlink(ip, -1);
  458. if (error)
  459. return error;
  460. return error;
  461. }
  462. /**
  463. * gfs2_rmdir - Remove a directory
  464. * @dir: The parent directory of the directory to be removed
  465. * @dentry: The dentry of the directory to remove
  466. *
  467. * Remove a directory. Call gfs2_rmdiri()
  468. *
  469. * Returns: errno
  470. */
  471. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  472. {
  473. struct gfs2_inode *dip = GFS2_I(dir);
  474. struct gfs2_sbd *sdp = GFS2_SB(dir);
  475. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  476. struct gfs2_holder ghs[3];
  477. struct gfs2_rgrpd *rgd;
  478. struct gfs2_holder ri_gh;
  479. int error;
  480. error = gfs2_rindex_hold(sdp, &ri_gh);
  481. if (error)
  482. return error;
  483. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  484. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  485. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  486. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  487. error = gfs2_glock_nq(ghs); /* parent */
  488. if (error)
  489. goto out_parent;
  490. error = gfs2_glock_nq(ghs + 1); /* child */
  491. if (error)
  492. goto out_child;
  493. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  494. if (error)
  495. goto out_rgrp;
  496. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  497. if (error)
  498. goto out_gunlock;
  499. if (ip->i_entries < 2) {
  500. if (gfs2_consist_inode(ip))
  501. gfs2_dinode_print(ip);
  502. error = -EIO;
  503. goto out_gunlock;
  504. }
  505. if (ip->i_entries > 2) {
  506. error = -ENOTEMPTY;
  507. goto out_gunlock;
  508. }
  509. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  510. if (error)
  511. goto out_gunlock;
  512. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  513. gfs2_trans_end(sdp);
  514. out_gunlock:
  515. gfs2_glock_dq(ghs + 2);
  516. out_rgrp:
  517. gfs2_holder_uninit(ghs + 2);
  518. gfs2_glock_dq(ghs + 1);
  519. out_child:
  520. gfs2_holder_uninit(ghs + 1);
  521. gfs2_glock_dq(ghs);
  522. out_parent:
  523. gfs2_holder_uninit(ghs);
  524. gfs2_glock_dq_uninit(&ri_gh);
  525. return error;
  526. }
  527. /**
  528. * gfs2_mknod - Make a special file
  529. * @dir: The directory in which the special file will reside
  530. * @dentry: The dentry of the special file
  531. * @mode: The mode of the special file
  532. * @rdev: The device specification of the special file
  533. *
  534. */
  535. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  536. dev_t dev)
  537. {
  538. struct gfs2_inode *dip = GFS2_I(dir);
  539. struct gfs2_sbd *sdp = GFS2_SB(dir);
  540. struct gfs2_holder ghs[2];
  541. struct inode *inode;
  542. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  543. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  544. if (IS_ERR(inode)) {
  545. gfs2_holder_uninit(ghs);
  546. return PTR_ERR(inode);
  547. }
  548. gfs2_trans_end(sdp);
  549. if (dip->i_alloc->al_rgd)
  550. gfs2_inplace_release(dip);
  551. gfs2_quota_unlock(dip);
  552. gfs2_alloc_put(dip);
  553. gfs2_glock_dq_uninit_m(2, ghs);
  554. d_instantiate(dentry, inode);
  555. mark_inode_dirty(inode);
  556. return 0;
  557. }
  558. /*
  559. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  560. * @this: move this
  561. * @to: to here
  562. *
  563. * Follow @to back to the root and make sure we don't encounter @this
  564. * Assumes we already hold the rename lock.
  565. *
  566. * Returns: errno
  567. */
  568. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  569. {
  570. struct inode *dir = &to->i_inode;
  571. struct super_block *sb = dir->i_sb;
  572. struct inode *tmp;
  573. int error = 0;
  574. igrab(dir);
  575. for (;;) {
  576. if (dir == &this->i_inode) {
  577. error = -EINVAL;
  578. break;
  579. }
  580. if (dir == sb->s_root->d_inode) {
  581. error = 0;
  582. break;
  583. }
  584. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  585. if (IS_ERR(tmp)) {
  586. error = PTR_ERR(tmp);
  587. break;
  588. }
  589. iput(dir);
  590. dir = tmp;
  591. }
  592. iput(dir);
  593. return error;
  594. }
  595. /**
  596. * gfs2_rename - Rename a file
  597. * @odir: Parent directory of old file name
  598. * @odentry: The old dentry of the file
  599. * @ndir: Parent directory of new file name
  600. * @ndentry: The new dentry of the file
  601. *
  602. * Returns: errno
  603. */
  604. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  605. struct inode *ndir, struct dentry *ndentry)
  606. {
  607. struct gfs2_inode *odip = GFS2_I(odir);
  608. struct gfs2_inode *ndip = GFS2_I(ndir);
  609. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  610. struct gfs2_inode *nip = NULL;
  611. struct gfs2_sbd *sdp = GFS2_SB(odir);
  612. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  613. struct gfs2_rgrpd *nrgd;
  614. unsigned int num_gh;
  615. int dir_rename = 0;
  616. int alloc_required = 0;
  617. unsigned int x;
  618. int error;
  619. if (ndentry->d_inode) {
  620. nip = GFS2_I(ndentry->d_inode);
  621. if (ip == nip)
  622. return 0;
  623. }
  624. error = gfs2_rindex_hold(sdp, &ri_gh);
  625. if (error)
  626. return error;
  627. if (odip != ndip) {
  628. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  629. 0, &r_gh);
  630. if (error)
  631. goto out;
  632. if (S_ISDIR(ip->i_inode.i_mode)) {
  633. dir_rename = 1;
  634. /* don't move a dirctory into it's subdir */
  635. error = gfs2_ok_to_move(ip, ndip);
  636. if (error)
  637. goto out_gunlock_r;
  638. }
  639. }
  640. num_gh = 1;
  641. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  642. if (odip != ndip) {
  643. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  644. num_gh++;
  645. }
  646. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  647. num_gh++;
  648. if (nip) {
  649. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  650. num_gh++;
  651. /* grab the resource lock for unlink flag twiddling
  652. * this is the case of the target file already existing
  653. * so we unlink before doing the rename
  654. */
  655. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  656. if (nrgd)
  657. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  658. }
  659. for (x = 0; x < num_gh; x++) {
  660. error = gfs2_glock_nq(ghs + x);
  661. if (error)
  662. goto out_gunlock;
  663. }
  664. /* Check out the old directory */
  665. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  666. if (error)
  667. goto out_gunlock;
  668. /* Check out the new directory */
  669. if (nip) {
  670. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  671. if (error)
  672. goto out_gunlock;
  673. if (S_ISDIR(nip->i_inode.i_mode)) {
  674. if (nip->i_entries < 2) {
  675. if (gfs2_consist_inode(nip))
  676. gfs2_dinode_print(nip);
  677. error = -EIO;
  678. goto out_gunlock;
  679. }
  680. if (nip->i_entries > 2) {
  681. error = -ENOTEMPTY;
  682. goto out_gunlock;
  683. }
  684. }
  685. } else {
  686. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  687. if (error)
  688. goto out_gunlock;
  689. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  690. switch (error) {
  691. case -ENOENT:
  692. error = 0;
  693. break;
  694. case 0:
  695. error = -EEXIST;
  696. default:
  697. goto out_gunlock;
  698. };
  699. if (odip != ndip) {
  700. if (!ndip->i_inode.i_nlink) {
  701. error = -EINVAL;
  702. goto out_gunlock;
  703. }
  704. if (ndip->i_entries == (u32)-1) {
  705. error = -EFBIG;
  706. goto out_gunlock;
  707. }
  708. if (S_ISDIR(ip->i_inode.i_mode) &&
  709. ndip->i_inode.i_nlink == (u32)-1) {
  710. error = -EMLINK;
  711. goto out_gunlock;
  712. }
  713. }
  714. }
  715. /* Check out the dir to be renamed */
  716. if (dir_rename) {
  717. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  718. if (error)
  719. goto out_gunlock;
  720. }
  721. if (nip == NULL)
  722. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  723. error = alloc_required;
  724. if (error < 0)
  725. goto out_gunlock;
  726. error = 0;
  727. if (alloc_required) {
  728. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  729. if (!al) {
  730. error = -ENOMEM;
  731. goto out_gunlock;
  732. }
  733. error = gfs2_quota_lock_check(ndip);
  734. if (error)
  735. goto out_alloc;
  736. al->al_requested = sdp->sd_max_dirres;
  737. error = gfs2_inplace_reserve_ri(ndip);
  738. if (error)
  739. goto out_gunlock_q;
  740. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  741. gfs2_rg_blocks(al) +
  742. 4 * RES_DINODE + 4 * RES_LEAF +
  743. RES_STATFS + RES_QUOTA + 4, 0);
  744. if (error)
  745. goto out_ipreserv;
  746. } else {
  747. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  748. 5 * RES_LEAF + 4, 0);
  749. if (error)
  750. goto out_gunlock;
  751. }
  752. /* Remove the target file, if it exists */
  753. if (nip) {
  754. if (S_ISDIR(nip->i_inode.i_mode))
  755. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  756. else {
  757. error = gfs2_dir_del(ndip, &ndentry->d_name);
  758. if (error)
  759. goto out_end_trans;
  760. error = gfs2_change_nlink(nip, -1);
  761. }
  762. if (error)
  763. goto out_end_trans;
  764. }
  765. if (dir_rename) {
  766. error = gfs2_change_nlink(ndip, +1);
  767. if (error)
  768. goto out_end_trans;
  769. error = gfs2_change_nlink(odip, -1);
  770. if (error)
  771. goto out_end_trans;
  772. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  773. if (error)
  774. goto out_end_trans;
  775. } else {
  776. struct buffer_head *dibh;
  777. error = gfs2_meta_inode_buffer(ip, &dibh);
  778. if (error)
  779. goto out_end_trans;
  780. ip->i_inode.i_ctime = CURRENT_TIME;
  781. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  782. gfs2_dinode_out(ip, dibh->b_data);
  783. brelse(dibh);
  784. }
  785. error = gfs2_dir_del(odip, &odentry->d_name);
  786. if (error)
  787. goto out_end_trans;
  788. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  789. if (error)
  790. goto out_end_trans;
  791. out_end_trans:
  792. gfs2_trans_end(sdp);
  793. out_ipreserv:
  794. if (alloc_required)
  795. gfs2_inplace_release(ndip);
  796. out_gunlock_q:
  797. if (alloc_required)
  798. gfs2_quota_unlock(ndip);
  799. out_alloc:
  800. if (alloc_required)
  801. gfs2_alloc_put(ndip);
  802. out_gunlock:
  803. while (x--) {
  804. gfs2_glock_dq(ghs + x);
  805. gfs2_holder_uninit(ghs + x);
  806. }
  807. out_gunlock_r:
  808. if (r_gh.gh_gl)
  809. gfs2_glock_dq_uninit(&r_gh);
  810. out:
  811. gfs2_glock_dq_uninit(&ri_gh);
  812. return error;
  813. }
  814. /**
  815. * gfs2_follow_link - Follow a symbolic link
  816. * @dentry: The dentry of the link
  817. * @nd: Data that we pass to vfs_follow_link()
  818. *
  819. * This can handle symlinks of any size.
  820. *
  821. * Returns: 0 on success or error code
  822. */
  823. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  824. {
  825. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  826. struct gfs2_holder i_gh;
  827. struct buffer_head *dibh;
  828. unsigned int x, size;
  829. char *buf;
  830. int error;
  831. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  832. error = gfs2_glock_nq(&i_gh);
  833. if (error) {
  834. gfs2_holder_uninit(&i_gh);
  835. nd_set_link(nd, ERR_PTR(error));
  836. return NULL;
  837. }
  838. size = (unsigned int)i_size_read(&ip->i_inode);
  839. if (size == 0) {
  840. gfs2_consist_inode(ip);
  841. buf = ERR_PTR(-EIO);
  842. goto out;
  843. }
  844. error = gfs2_meta_inode_buffer(ip, &dibh);
  845. if (error) {
  846. buf = ERR_PTR(error);
  847. goto out;
  848. }
  849. x = size + 1;
  850. buf = kmalloc(x, GFP_NOFS);
  851. if (!buf)
  852. buf = ERR_PTR(-ENOMEM);
  853. else
  854. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  855. brelse(dibh);
  856. out:
  857. gfs2_glock_dq_uninit(&i_gh);
  858. nd_set_link(nd, buf);
  859. return NULL;
  860. }
  861. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  862. {
  863. char *s = nd_get_link(nd);
  864. if (!IS_ERR(s))
  865. kfree(s);
  866. }
  867. /**
  868. * gfs2_permission -
  869. * @inode:
  870. * @mask:
  871. * @nd: passed from Linux VFS, ignored by us
  872. *
  873. * This may be called from the VFS directly, or from within GFS2 with the
  874. * inode locked, so we look to see if the glock is already locked and only
  875. * lock the glock if its not already been done.
  876. *
  877. * Returns: errno
  878. */
  879. int gfs2_permission(struct inode *inode, int mask)
  880. {
  881. struct gfs2_inode *ip = GFS2_I(inode);
  882. struct gfs2_holder i_gh;
  883. int error;
  884. int unlock = 0;
  885. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  886. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  887. if (error)
  888. return error;
  889. unlock = 1;
  890. }
  891. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  892. error = -EACCES;
  893. else
  894. error = generic_permission(inode, mask, gfs2_check_acl);
  895. if (unlock)
  896. gfs2_glock_dq_uninit(&i_gh);
  897. return error;
  898. }
  899. static int setattr_chown(struct inode *inode, struct iattr *attr)
  900. {
  901. struct gfs2_inode *ip = GFS2_I(inode);
  902. struct gfs2_sbd *sdp = GFS2_SB(inode);
  903. u32 ouid, ogid, nuid, ngid;
  904. int error;
  905. ouid = inode->i_uid;
  906. ogid = inode->i_gid;
  907. nuid = attr->ia_uid;
  908. ngid = attr->ia_gid;
  909. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  910. ouid = nuid = NO_QUOTA_CHANGE;
  911. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  912. ogid = ngid = NO_QUOTA_CHANGE;
  913. if (!gfs2_alloc_get(ip))
  914. return -ENOMEM;
  915. error = gfs2_quota_lock(ip, nuid, ngid);
  916. if (error)
  917. goto out_alloc;
  918. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  919. error = gfs2_quota_check(ip, nuid, ngid);
  920. if (error)
  921. goto out_gunlock_q;
  922. }
  923. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  924. if (error)
  925. goto out_gunlock_q;
  926. error = gfs2_setattr_simple(ip, attr);
  927. if (error)
  928. goto out_end_trans;
  929. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  930. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  931. gfs2_quota_change(ip, -blocks, ouid, ogid);
  932. gfs2_quota_change(ip, blocks, nuid, ngid);
  933. }
  934. out_end_trans:
  935. gfs2_trans_end(sdp);
  936. out_gunlock_q:
  937. gfs2_quota_unlock(ip);
  938. out_alloc:
  939. gfs2_alloc_put(ip);
  940. return error;
  941. }
  942. /**
  943. * gfs2_setattr - Change attributes on an inode
  944. * @dentry: The dentry which is changing
  945. * @attr: The structure describing the change
  946. *
  947. * The VFS layer wants to change one or more of an inodes attributes. Write
  948. * that change out to disk.
  949. *
  950. * Returns: errno
  951. */
  952. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  953. {
  954. struct inode *inode = dentry->d_inode;
  955. struct gfs2_inode *ip = GFS2_I(inode);
  956. struct gfs2_holder i_gh;
  957. int error;
  958. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  959. if (error)
  960. return error;
  961. error = -EPERM;
  962. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  963. goto out;
  964. error = inode_change_ok(inode, attr);
  965. if (error)
  966. goto out;
  967. if (attr->ia_valid & ATTR_SIZE)
  968. error = gfs2_setattr_size(inode, attr->ia_size);
  969. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  970. error = setattr_chown(inode, attr);
  971. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  972. error = gfs2_acl_chmod(ip, attr);
  973. else
  974. error = gfs2_setattr_simple(ip, attr);
  975. out:
  976. gfs2_glock_dq_uninit(&i_gh);
  977. if (!error)
  978. mark_inode_dirty(inode);
  979. return error;
  980. }
  981. /**
  982. * gfs2_getattr - Read out an inode's attributes
  983. * @mnt: The vfsmount the inode is being accessed from
  984. * @dentry: The dentry to stat
  985. * @stat: The inode's stats
  986. *
  987. * This may be called from the VFS directly, or from within GFS2 with the
  988. * inode locked, so we look to see if the glock is already locked and only
  989. * lock the glock if its not already been done. Note that its the NFS
  990. * readdirplus operation which causes this to be called (from filldir)
  991. * with the glock already held.
  992. *
  993. * Returns: errno
  994. */
  995. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  996. struct kstat *stat)
  997. {
  998. struct inode *inode = dentry->d_inode;
  999. struct gfs2_inode *ip = GFS2_I(inode);
  1000. struct gfs2_holder gh;
  1001. int error;
  1002. int unlock = 0;
  1003. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1004. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1005. if (error)
  1006. return error;
  1007. unlock = 1;
  1008. }
  1009. generic_fillattr(inode, stat);
  1010. if (unlock)
  1011. gfs2_glock_dq_uninit(&gh);
  1012. return 0;
  1013. }
  1014. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1015. const void *data, size_t size, int flags)
  1016. {
  1017. struct inode *inode = dentry->d_inode;
  1018. struct gfs2_inode *ip = GFS2_I(inode);
  1019. struct gfs2_holder gh;
  1020. int ret;
  1021. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1022. ret = gfs2_glock_nq(&gh);
  1023. if (ret == 0) {
  1024. ret = generic_setxattr(dentry, name, data, size, flags);
  1025. gfs2_glock_dq(&gh);
  1026. }
  1027. gfs2_holder_uninit(&gh);
  1028. return ret;
  1029. }
  1030. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1031. void *data, size_t size)
  1032. {
  1033. struct inode *inode = dentry->d_inode;
  1034. struct gfs2_inode *ip = GFS2_I(inode);
  1035. struct gfs2_holder gh;
  1036. int ret;
  1037. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1038. ret = gfs2_glock_nq(&gh);
  1039. if (ret == 0) {
  1040. ret = generic_getxattr(dentry, name, data, size);
  1041. gfs2_glock_dq(&gh);
  1042. }
  1043. gfs2_holder_uninit(&gh);
  1044. return ret;
  1045. }
  1046. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1047. {
  1048. struct inode *inode = dentry->d_inode;
  1049. struct gfs2_inode *ip = GFS2_I(inode);
  1050. struct gfs2_holder gh;
  1051. int ret;
  1052. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1053. ret = gfs2_glock_nq(&gh);
  1054. if (ret == 0) {
  1055. ret = generic_removexattr(dentry, name);
  1056. gfs2_glock_dq(&gh);
  1057. }
  1058. gfs2_holder_uninit(&gh);
  1059. return ret;
  1060. }
  1061. static void empty_write_end(struct page *page, unsigned from,
  1062. unsigned to)
  1063. {
  1064. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  1065. page_zero_new_buffers(page, from, to);
  1066. flush_dcache_page(page);
  1067. mark_page_accessed(page);
  1068. if (!gfs2_is_writeback(ip))
  1069. gfs2_page_add_databufs(ip, page, from, to);
  1070. block_commit_write(page, from, to);
  1071. }
  1072. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  1073. {
  1074. unsigned start, end, next;
  1075. struct buffer_head *bh, *head;
  1076. int error;
  1077. if (!page_has_buffers(page)) {
  1078. error = __block_write_begin(page, from, to - from, gfs2_block_map);
  1079. if (unlikely(error))
  1080. return error;
  1081. empty_write_end(page, from, to);
  1082. return 0;
  1083. }
  1084. bh = head = page_buffers(page);
  1085. next = end = 0;
  1086. while (next < from) {
  1087. next += bh->b_size;
  1088. bh = bh->b_this_page;
  1089. }
  1090. start = next;
  1091. do {
  1092. next += bh->b_size;
  1093. if (buffer_mapped(bh)) {
  1094. if (end) {
  1095. error = __block_write_begin(page, start, end - start,
  1096. gfs2_block_map);
  1097. if (unlikely(error))
  1098. return error;
  1099. empty_write_end(page, start, end);
  1100. end = 0;
  1101. }
  1102. start = next;
  1103. }
  1104. else
  1105. end = next;
  1106. bh = bh->b_this_page;
  1107. } while (next < to);
  1108. if (end) {
  1109. error = __block_write_begin(page, start, end - start, gfs2_block_map);
  1110. if (unlikely(error))
  1111. return error;
  1112. empty_write_end(page, start, end);
  1113. }
  1114. return 0;
  1115. }
  1116. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1117. int mode)
  1118. {
  1119. struct gfs2_inode *ip = GFS2_I(inode);
  1120. struct buffer_head *dibh;
  1121. int error;
  1122. u64 start = offset >> PAGE_CACHE_SHIFT;
  1123. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  1124. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  1125. pgoff_t curr;
  1126. struct page *page;
  1127. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  1128. unsigned int from, to;
  1129. if (!end_offset)
  1130. end_offset = PAGE_CACHE_SIZE;
  1131. error = gfs2_meta_inode_buffer(ip, &dibh);
  1132. if (unlikely(error))
  1133. goto out;
  1134. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1135. if (gfs2_is_stuffed(ip)) {
  1136. error = gfs2_unstuff_dinode(ip, NULL);
  1137. if (unlikely(error))
  1138. goto out;
  1139. }
  1140. curr = start;
  1141. offset = start << PAGE_CACHE_SHIFT;
  1142. from = start_offset;
  1143. to = PAGE_CACHE_SIZE;
  1144. while (curr <= end) {
  1145. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  1146. AOP_FLAG_NOFS);
  1147. if (unlikely(!page)) {
  1148. error = -ENOMEM;
  1149. goto out;
  1150. }
  1151. if (curr == end)
  1152. to = end_offset;
  1153. error = write_empty_blocks(page, from, to);
  1154. if (!error && offset + to > inode->i_size &&
  1155. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1156. i_size_write(inode, offset + to);
  1157. }
  1158. unlock_page(page);
  1159. page_cache_release(page);
  1160. if (error)
  1161. goto out;
  1162. curr++;
  1163. offset += PAGE_CACHE_SIZE;
  1164. from = 0;
  1165. }
  1166. gfs2_dinode_out(ip, dibh->b_data);
  1167. mark_inode_dirty(inode);
  1168. brelse(dibh);
  1169. out:
  1170. return error;
  1171. }
  1172. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  1173. unsigned int *data_blocks, unsigned int *ind_blocks)
  1174. {
  1175. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1176. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  1177. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1178. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1179. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1180. max_data -= tmp;
  1181. }
  1182. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  1183. so it might end up with fewer data blocks */
  1184. if (max_data <= *data_blocks)
  1185. return;
  1186. *data_blocks = max_data;
  1187. *ind_blocks = max_blocks - max_data;
  1188. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1189. if (*len > max) {
  1190. *len = max;
  1191. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1192. }
  1193. }
  1194. static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1195. loff_t len)
  1196. {
  1197. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1198. struct gfs2_inode *ip = GFS2_I(inode);
  1199. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1200. loff_t bytes, max_bytes;
  1201. struct gfs2_alloc *al;
  1202. int error;
  1203. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1204. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1205. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  1206. sdp->sd_sb.sb_bsize_shift;
  1207. len = next - offset;
  1208. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1209. if (!bytes)
  1210. bytes = UINT_MAX;
  1211. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  1212. error = gfs2_glock_nq(&ip->i_gh);
  1213. if (unlikely(error))
  1214. goto out_uninit;
  1215. if (!gfs2_write_alloc_required(ip, offset, len))
  1216. goto out_unlock;
  1217. while (len > 0) {
  1218. if (len < bytes)
  1219. bytes = len;
  1220. al = gfs2_alloc_get(ip);
  1221. if (!al) {
  1222. error = -ENOMEM;
  1223. goto out_unlock;
  1224. }
  1225. error = gfs2_quota_lock_check(ip);
  1226. if (error)
  1227. goto out_alloc_put;
  1228. retry:
  1229. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1230. al->al_requested = data_blocks + ind_blocks;
  1231. error = gfs2_inplace_reserve(ip);
  1232. if (error) {
  1233. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  1234. bytes >>= 1;
  1235. goto retry;
  1236. }
  1237. goto out_qunlock;
  1238. }
  1239. max_bytes = bytes;
  1240. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  1241. al->al_requested = data_blocks + ind_blocks;
  1242. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1243. RES_RG_HDR + gfs2_rg_blocks(al);
  1244. if (gfs2_is_jdata(ip))
  1245. rblocks += data_blocks ? data_blocks : 1;
  1246. error = gfs2_trans_begin(sdp, rblocks,
  1247. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  1248. if (error)
  1249. goto out_trans_fail;
  1250. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1251. gfs2_trans_end(sdp);
  1252. if (error)
  1253. goto out_trans_fail;
  1254. len -= max_bytes;
  1255. offset += max_bytes;
  1256. gfs2_inplace_release(ip);
  1257. gfs2_quota_unlock(ip);
  1258. gfs2_alloc_put(ip);
  1259. }
  1260. goto out_unlock;
  1261. out_trans_fail:
  1262. gfs2_inplace_release(ip);
  1263. out_qunlock:
  1264. gfs2_quota_unlock(ip);
  1265. out_alloc_put:
  1266. gfs2_alloc_put(ip);
  1267. out_unlock:
  1268. gfs2_glock_dq(&ip->i_gh);
  1269. out_uninit:
  1270. gfs2_holder_uninit(&ip->i_gh);
  1271. return error;
  1272. }
  1273. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1274. u64 start, u64 len)
  1275. {
  1276. struct gfs2_inode *ip = GFS2_I(inode);
  1277. struct gfs2_holder gh;
  1278. int ret;
  1279. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1280. if (ret)
  1281. return ret;
  1282. mutex_lock(&inode->i_mutex);
  1283. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1284. if (ret)
  1285. goto out;
  1286. if (gfs2_is_stuffed(ip)) {
  1287. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1288. u64 size = i_size_read(inode);
  1289. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1290. FIEMAP_EXTENT_DATA_INLINE;
  1291. phys += sizeof(struct gfs2_dinode);
  1292. phys += start;
  1293. if (start + len > size)
  1294. len = size - start;
  1295. if (start < size)
  1296. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1297. len, flags);
  1298. if (ret == 1)
  1299. ret = 0;
  1300. } else {
  1301. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1302. gfs2_block_map);
  1303. }
  1304. gfs2_glock_dq_uninit(&gh);
  1305. out:
  1306. mutex_unlock(&inode->i_mutex);
  1307. return ret;
  1308. }
  1309. const struct inode_operations gfs2_file_iops = {
  1310. .permission = gfs2_permission,
  1311. .setattr = gfs2_setattr,
  1312. .getattr = gfs2_getattr,
  1313. .setxattr = gfs2_setxattr,
  1314. .getxattr = gfs2_getxattr,
  1315. .listxattr = gfs2_listxattr,
  1316. .removexattr = gfs2_removexattr,
  1317. .fallocate = gfs2_fallocate,
  1318. .fiemap = gfs2_fiemap,
  1319. };
  1320. const struct inode_operations gfs2_dir_iops = {
  1321. .create = gfs2_create,
  1322. .lookup = gfs2_lookup,
  1323. .link = gfs2_link,
  1324. .unlink = gfs2_unlink,
  1325. .symlink = gfs2_symlink,
  1326. .mkdir = gfs2_mkdir,
  1327. .rmdir = gfs2_rmdir,
  1328. .mknod = gfs2_mknod,
  1329. .rename = gfs2_rename,
  1330. .permission = gfs2_permission,
  1331. .setattr = gfs2_setattr,
  1332. .getattr = gfs2_getattr,
  1333. .setxattr = gfs2_setxattr,
  1334. .getxattr = gfs2_getxattr,
  1335. .listxattr = gfs2_listxattr,
  1336. .removexattr = gfs2_removexattr,
  1337. .fiemap = gfs2_fiemap,
  1338. };
  1339. const struct inode_operations gfs2_symlink_iops = {
  1340. .readlink = generic_readlink,
  1341. .follow_link = gfs2_follow_link,
  1342. .put_link = gfs2_put_link,
  1343. .permission = gfs2_permission,
  1344. .setattr = gfs2_setattr,
  1345. .getattr = gfs2_getattr,
  1346. .setxattr = gfs2_setxattr,
  1347. .getxattr = gfs2_getxattr,
  1348. .listxattr = gfs2_listxattr,
  1349. .removexattr = gfs2_removexattr,
  1350. .fiemap = gfs2_fiemap,
  1351. };