ops_inode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  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. al->al_rgd->rd_length +
  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. atomic_inc(&inode->i_count);
  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, };
  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. if (odip != ndip) {
  625. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  626. 0, &r_gh);
  627. if (error)
  628. goto out;
  629. if (S_ISDIR(ip->i_inode.i_mode)) {
  630. dir_rename = 1;
  631. /* don't move a dirctory into it's subdir */
  632. error = gfs2_ok_to_move(ip, ndip);
  633. if (error)
  634. goto out_gunlock_r;
  635. }
  636. }
  637. num_gh = 1;
  638. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  639. if (odip != ndip) {
  640. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  641. num_gh++;
  642. }
  643. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  644. num_gh++;
  645. if (nip) {
  646. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  647. num_gh++;
  648. /* grab the resource lock for unlink flag twiddling
  649. * this is the case of the target file already existing
  650. * so we unlink before doing the rename
  651. */
  652. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  653. if (nrgd)
  654. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  655. }
  656. for (x = 0; x < num_gh; x++) {
  657. error = gfs2_glock_nq(ghs + x);
  658. if (error)
  659. goto out_gunlock;
  660. }
  661. /* Check out the old directory */
  662. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  663. if (error)
  664. goto out_gunlock;
  665. /* Check out the new directory */
  666. if (nip) {
  667. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  668. if (error)
  669. goto out_gunlock;
  670. if (S_ISDIR(nip->i_inode.i_mode)) {
  671. if (nip->i_entries < 2) {
  672. if (gfs2_consist_inode(nip))
  673. gfs2_dinode_print(nip);
  674. error = -EIO;
  675. goto out_gunlock;
  676. }
  677. if (nip->i_entries > 2) {
  678. error = -ENOTEMPTY;
  679. goto out_gunlock;
  680. }
  681. }
  682. } else {
  683. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  684. if (error)
  685. goto out_gunlock;
  686. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  687. switch (error) {
  688. case -ENOENT:
  689. error = 0;
  690. break;
  691. case 0:
  692. error = -EEXIST;
  693. default:
  694. goto out_gunlock;
  695. };
  696. if (odip != ndip) {
  697. if (!ndip->i_inode.i_nlink) {
  698. error = -EINVAL;
  699. goto out_gunlock;
  700. }
  701. if (ndip->i_entries == (u32)-1) {
  702. error = -EFBIG;
  703. goto out_gunlock;
  704. }
  705. if (S_ISDIR(ip->i_inode.i_mode) &&
  706. ndip->i_inode.i_nlink == (u32)-1) {
  707. error = -EMLINK;
  708. goto out_gunlock;
  709. }
  710. }
  711. }
  712. /* Check out the dir to be renamed */
  713. if (dir_rename) {
  714. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  715. if (error)
  716. goto out_gunlock;
  717. }
  718. if (nip == NULL)
  719. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  720. error = alloc_required;
  721. if (error < 0)
  722. goto out_gunlock;
  723. error = 0;
  724. if (alloc_required) {
  725. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  726. if (!al) {
  727. error = -ENOMEM;
  728. goto out_gunlock;
  729. }
  730. error = gfs2_quota_lock_check(ndip);
  731. if (error)
  732. goto out_alloc;
  733. al->al_requested = sdp->sd_max_dirres;
  734. error = gfs2_inplace_reserve(ndip);
  735. if (error)
  736. goto out_gunlock_q;
  737. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  738. al->al_rgd->rd_length +
  739. 4 * RES_DINODE + 4 * RES_LEAF +
  740. RES_STATFS + RES_QUOTA + 4, 0);
  741. if (error)
  742. goto out_ipreserv;
  743. } else {
  744. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  745. 5 * RES_LEAF + 4, 0);
  746. if (error)
  747. goto out_gunlock;
  748. }
  749. /* Remove the target file, if it exists */
  750. if (nip) {
  751. if (S_ISDIR(nip->i_inode.i_mode))
  752. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  753. else {
  754. error = gfs2_dir_del(ndip, &ndentry->d_name);
  755. if (error)
  756. goto out_end_trans;
  757. error = gfs2_change_nlink(nip, -1);
  758. }
  759. if (error)
  760. goto out_end_trans;
  761. }
  762. if (dir_rename) {
  763. error = gfs2_change_nlink(ndip, +1);
  764. if (error)
  765. goto out_end_trans;
  766. error = gfs2_change_nlink(odip, -1);
  767. if (error)
  768. goto out_end_trans;
  769. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  770. if (error)
  771. goto out_end_trans;
  772. } else {
  773. struct buffer_head *dibh;
  774. error = gfs2_meta_inode_buffer(ip, &dibh);
  775. if (error)
  776. goto out_end_trans;
  777. ip->i_inode.i_ctime = CURRENT_TIME;
  778. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  779. gfs2_dinode_out(ip, dibh->b_data);
  780. brelse(dibh);
  781. }
  782. error = gfs2_dir_del(odip, &odentry->d_name);
  783. if (error)
  784. goto out_end_trans;
  785. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  786. if (error)
  787. goto out_end_trans;
  788. out_end_trans:
  789. gfs2_trans_end(sdp);
  790. out_ipreserv:
  791. if (alloc_required)
  792. gfs2_inplace_release(ndip);
  793. out_gunlock_q:
  794. if (alloc_required)
  795. gfs2_quota_unlock(ndip);
  796. out_alloc:
  797. if (alloc_required)
  798. gfs2_alloc_put(ndip);
  799. out_gunlock:
  800. while (x--) {
  801. gfs2_glock_dq(ghs + x);
  802. gfs2_holder_uninit(ghs + x);
  803. }
  804. out_gunlock_r:
  805. if (r_gh.gh_gl)
  806. gfs2_glock_dq_uninit(&r_gh);
  807. out:
  808. return error;
  809. }
  810. /**
  811. * gfs2_follow_link - Follow a symbolic link
  812. * @dentry: The dentry of the link
  813. * @nd: Data that we pass to vfs_follow_link()
  814. *
  815. * This can handle symlinks of any size.
  816. *
  817. * Returns: 0 on success or error code
  818. */
  819. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  820. {
  821. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  822. struct gfs2_holder i_gh;
  823. struct buffer_head *dibh;
  824. unsigned int x, size;
  825. char *buf;
  826. int error;
  827. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  828. error = gfs2_glock_nq(&i_gh);
  829. if (error) {
  830. gfs2_holder_uninit(&i_gh);
  831. nd_set_link(nd, ERR_PTR(error));
  832. return NULL;
  833. }
  834. size = (unsigned int)i_size_read(&ip->i_inode);
  835. if (size == 0) {
  836. gfs2_consist_inode(ip);
  837. buf = ERR_PTR(-EIO);
  838. goto out;
  839. }
  840. error = gfs2_meta_inode_buffer(ip, &dibh);
  841. if (error) {
  842. buf = ERR_PTR(error);
  843. goto out;
  844. }
  845. x = size + 1;
  846. buf = kmalloc(x, GFP_NOFS);
  847. if (!buf)
  848. buf = ERR_PTR(-ENOMEM);
  849. else
  850. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  851. brelse(dibh);
  852. out:
  853. gfs2_glock_dq_uninit(&i_gh);
  854. nd_set_link(nd, buf);
  855. return NULL;
  856. }
  857. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  858. {
  859. char *s = nd_get_link(nd);
  860. if (!IS_ERR(s))
  861. kfree(s);
  862. }
  863. /**
  864. * gfs2_permission -
  865. * @inode:
  866. * @mask:
  867. * @nd: passed from Linux VFS, ignored by us
  868. *
  869. * This may be called from the VFS directly, or from within GFS2 with the
  870. * inode locked, so we look to see if the glock is already locked and only
  871. * lock the glock if its not already been done.
  872. *
  873. * Returns: errno
  874. */
  875. int gfs2_permission(struct inode *inode, int mask)
  876. {
  877. struct gfs2_inode *ip = GFS2_I(inode);
  878. struct gfs2_holder i_gh;
  879. int error;
  880. int unlock = 0;
  881. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  882. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  883. if (error)
  884. return error;
  885. unlock = 1;
  886. }
  887. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  888. error = -EACCES;
  889. else
  890. error = generic_permission(inode, mask, gfs2_check_acl);
  891. if (unlock)
  892. gfs2_glock_dq_uninit(&i_gh);
  893. return error;
  894. }
  895. static int setattr_chown(struct inode *inode, struct iattr *attr)
  896. {
  897. struct gfs2_inode *ip = GFS2_I(inode);
  898. struct gfs2_sbd *sdp = GFS2_SB(inode);
  899. struct buffer_head *dibh;
  900. u32 ouid, ogid, nuid, ngid;
  901. int error;
  902. ouid = inode->i_uid;
  903. ogid = inode->i_gid;
  904. nuid = attr->ia_uid;
  905. ngid = attr->ia_gid;
  906. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  907. ouid = nuid = NO_QUOTA_CHANGE;
  908. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  909. ogid = ngid = NO_QUOTA_CHANGE;
  910. if (!gfs2_alloc_get(ip))
  911. return -ENOMEM;
  912. error = gfs2_quota_lock(ip, nuid, ngid);
  913. if (error)
  914. goto out_alloc;
  915. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  916. error = gfs2_quota_check(ip, nuid, ngid);
  917. if (error)
  918. goto out_gunlock_q;
  919. }
  920. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  921. if (error)
  922. goto out_gunlock_q;
  923. error = gfs2_meta_inode_buffer(ip, &dibh);
  924. if (error)
  925. goto out_end_trans;
  926. if ((attr->ia_valid & ATTR_SIZE) &&
  927. attr->ia_size != i_size_read(inode)) {
  928. int error;
  929. error = vmtruncate(inode, attr->ia_size);
  930. gfs2_assert_warn(sdp, !error);
  931. }
  932. setattr_copy(inode, attr);
  933. mark_inode_dirty(inode);
  934. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  935. gfs2_dinode_out(ip, dibh->b_data);
  936. brelse(dibh);
  937. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  938. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  939. gfs2_quota_change(ip, -blocks, ouid, ogid);
  940. gfs2_quota_change(ip, blocks, nuid, ngid);
  941. }
  942. out_end_trans:
  943. gfs2_trans_end(sdp);
  944. out_gunlock_q:
  945. gfs2_quota_unlock(ip);
  946. out_alloc:
  947. gfs2_alloc_put(ip);
  948. return error;
  949. }
  950. /**
  951. * gfs2_setattr - Change attributes on an inode
  952. * @dentry: The dentry which is changing
  953. * @attr: The structure describing the change
  954. *
  955. * The VFS layer wants to change one or more of an inodes attributes. Write
  956. * that change out to disk.
  957. *
  958. * Returns: errno
  959. */
  960. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  961. {
  962. struct inode *inode = dentry->d_inode;
  963. struct gfs2_inode *ip = GFS2_I(inode);
  964. struct gfs2_holder i_gh;
  965. int error;
  966. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  967. if (error)
  968. return error;
  969. error = -EPERM;
  970. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  971. goto out;
  972. error = inode_change_ok(inode, attr);
  973. if (error)
  974. goto out;
  975. if (attr->ia_valid & ATTR_SIZE)
  976. error = gfs2_setattr_size(inode, attr->ia_size);
  977. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  978. error = setattr_chown(inode, attr);
  979. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  980. error = gfs2_acl_chmod(ip, attr);
  981. else
  982. error = gfs2_setattr_simple(ip, attr);
  983. out:
  984. gfs2_glock_dq_uninit(&i_gh);
  985. if (!error)
  986. mark_inode_dirty(inode);
  987. return error;
  988. }
  989. /**
  990. * gfs2_getattr - Read out an inode's attributes
  991. * @mnt: The vfsmount the inode is being accessed from
  992. * @dentry: The dentry to stat
  993. * @stat: The inode's stats
  994. *
  995. * This may be called from the VFS directly, or from within GFS2 with the
  996. * inode locked, so we look to see if the glock is already locked and only
  997. * lock the glock if its not already been done. Note that its the NFS
  998. * readdirplus operation which causes this to be called (from filldir)
  999. * with the glock already held.
  1000. *
  1001. * Returns: errno
  1002. */
  1003. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1004. struct kstat *stat)
  1005. {
  1006. struct inode *inode = dentry->d_inode;
  1007. struct gfs2_inode *ip = GFS2_I(inode);
  1008. struct gfs2_holder gh;
  1009. int error;
  1010. int unlock = 0;
  1011. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1012. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1013. if (error)
  1014. return error;
  1015. unlock = 1;
  1016. }
  1017. generic_fillattr(inode, stat);
  1018. if (unlock)
  1019. gfs2_glock_dq_uninit(&gh);
  1020. return 0;
  1021. }
  1022. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1023. const void *data, size_t size, int flags)
  1024. {
  1025. struct inode *inode = dentry->d_inode;
  1026. struct gfs2_inode *ip = GFS2_I(inode);
  1027. struct gfs2_holder gh;
  1028. int ret;
  1029. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1030. ret = gfs2_glock_nq(&gh);
  1031. if (ret == 0) {
  1032. ret = generic_setxattr(dentry, name, data, size, flags);
  1033. gfs2_glock_dq(&gh);
  1034. }
  1035. gfs2_holder_uninit(&gh);
  1036. return ret;
  1037. }
  1038. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1039. void *data, size_t size)
  1040. {
  1041. struct inode *inode = dentry->d_inode;
  1042. struct gfs2_inode *ip = GFS2_I(inode);
  1043. struct gfs2_holder gh;
  1044. int ret;
  1045. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1046. ret = gfs2_glock_nq(&gh);
  1047. if (ret == 0) {
  1048. ret = generic_getxattr(dentry, name, data, size);
  1049. gfs2_glock_dq(&gh);
  1050. }
  1051. gfs2_holder_uninit(&gh);
  1052. return ret;
  1053. }
  1054. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1055. {
  1056. struct inode *inode = dentry->d_inode;
  1057. struct gfs2_inode *ip = GFS2_I(inode);
  1058. struct gfs2_holder gh;
  1059. int ret;
  1060. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1061. ret = gfs2_glock_nq(&gh);
  1062. if (ret == 0) {
  1063. ret = generic_removexattr(dentry, name);
  1064. gfs2_glock_dq(&gh);
  1065. }
  1066. gfs2_holder_uninit(&gh);
  1067. return ret;
  1068. }
  1069. static void empty_write_end(struct page *page, unsigned from,
  1070. unsigned to)
  1071. {
  1072. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  1073. page_zero_new_buffers(page, from, to);
  1074. flush_dcache_page(page);
  1075. mark_page_accessed(page);
  1076. if (!gfs2_is_writeback(ip))
  1077. gfs2_page_add_databufs(ip, page, from, to);
  1078. block_commit_write(page, from, to);
  1079. }
  1080. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  1081. {
  1082. unsigned start, end, next;
  1083. struct buffer_head *bh, *head;
  1084. int error;
  1085. if (!page_has_buffers(page)) {
  1086. error = block_prepare_write(page, from, to, gfs2_block_map);
  1087. if (unlikely(error))
  1088. return error;
  1089. empty_write_end(page, from, to);
  1090. return 0;
  1091. }
  1092. bh = head = page_buffers(page);
  1093. next = end = 0;
  1094. while (next < from) {
  1095. next += bh->b_size;
  1096. bh = bh->b_this_page;
  1097. }
  1098. start = next;
  1099. do {
  1100. next += bh->b_size;
  1101. if (buffer_mapped(bh)) {
  1102. if (end) {
  1103. error = block_prepare_write(page, start, end,
  1104. gfs2_block_map);
  1105. if (unlikely(error))
  1106. return error;
  1107. empty_write_end(page, start, end);
  1108. end = 0;
  1109. }
  1110. start = next;
  1111. }
  1112. else
  1113. end = next;
  1114. bh = bh->b_this_page;
  1115. } while (next < to);
  1116. if (end) {
  1117. error = block_prepare_write(page, start, end, gfs2_block_map);
  1118. if (unlikely(error))
  1119. return error;
  1120. empty_write_end(page, start, end);
  1121. }
  1122. return 0;
  1123. }
  1124. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1125. int mode)
  1126. {
  1127. struct gfs2_inode *ip = GFS2_I(inode);
  1128. struct buffer_head *dibh;
  1129. int error;
  1130. u64 start = offset >> PAGE_CACHE_SHIFT;
  1131. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  1132. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  1133. pgoff_t curr;
  1134. struct page *page;
  1135. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  1136. unsigned int from, to;
  1137. if (!end_offset)
  1138. end_offset = PAGE_CACHE_SIZE;
  1139. error = gfs2_meta_inode_buffer(ip, &dibh);
  1140. if (unlikely(error))
  1141. goto out;
  1142. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1143. if (gfs2_is_stuffed(ip)) {
  1144. error = gfs2_unstuff_dinode(ip, NULL);
  1145. if (unlikely(error))
  1146. goto out;
  1147. }
  1148. curr = start;
  1149. offset = start << PAGE_CACHE_SHIFT;
  1150. from = start_offset;
  1151. to = PAGE_CACHE_SIZE;
  1152. while (curr <= end) {
  1153. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  1154. AOP_FLAG_NOFS);
  1155. if (unlikely(!page)) {
  1156. error = -ENOMEM;
  1157. goto out;
  1158. }
  1159. if (curr == end)
  1160. to = end_offset;
  1161. error = write_empty_blocks(page, from, to);
  1162. if (!error && offset + to > inode->i_size &&
  1163. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1164. i_size_write(inode, offset + to);
  1165. }
  1166. unlock_page(page);
  1167. page_cache_release(page);
  1168. if (error)
  1169. goto out;
  1170. curr++;
  1171. offset += PAGE_CACHE_SIZE;
  1172. from = 0;
  1173. }
  1174. gfs2_dinode_out(ip, dibh->b_data);
  1175. mark_inode_dirty(inode);
  1176. brelse(dibh);
  1177. out:
  1178. return error;
  1179. }
  1180. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  1181. unsigned int *data_blocks, unsigned int *ind_blocks)
  1182. {
  1183. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1184. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  1185. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1186. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1187. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1188. max_data -= tmp;
  1189. }
  1190. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  1191. so it might end up with fewer data blocks */
  1192. if (max_data <= *data_blocks)
  1193. return;
  1194. *data_blocks = max_data;
  1195. *ind_blocks = max_blocks - max_data;
  1196. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1197. if (*len > max) {
  1198. *len = max;
  1199. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1200. }
  1201. }
  1202. static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1203. loff_t len)
  1204. {
  1205. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1206. struct gfs2_inode *ip = GFS2_I(inode);
  1207. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1208. loff_t bytes, max_bytes;
  1209. struct gfs2_alloc *al;
  1210. int error;
  1211. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1212. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1213. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  1214. sdp->sd_sb.sb_bsize_shift;
  1215. len = next - offset;
  1216. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1217. if (!bytes)
  1218. bytes = UINT_MAX;
  1219. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  1220. error = gfs2_glock_nq(&ip->i_gh);
  1221. if (unlikely(error))
  1222. goto out_uninit;
  1223. if (!gfs2_write_alloc_required(ip, offset, len))
  1224. goto out_unlock;
  1225. while (len > 0) {
  1226. if (len < bytes)
  1227. bytes = len;
  1228. al = gfs2_alloc_get(ip);
  1229. if (!al) {
  1230. error = -ENOMEM;
  1231. goto out_unlock;
  1232. }
  1233. error = gfs2_quota_lock_check(ip);
  1234. if (error)
  1235. goto out_alloc_put;
  1236. retry:
  1237. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1238. al->al_requested = data_blocks + ind_blocks;
  1239. error = gfs2_inplace_reserve(ip);
  1240. if (error) {
  1241. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  1242. bytes >>= 1;
  1243. goto retry;
  1244. }
  1245. goto out_qunlock;
  1246. }
  1247. max_bytes = bytes;
  1248. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  1249. al->al_requested = data_blocks + ind_blocks;
  1250. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1251. RES_RG_HDR + ip->i_alloc->al_rgd->rd_length;
  1252. if (gfs2_is_jdata(ip))
  1253. rblocks += data_blocks ? data_blocks : 1;
  1254. error = gfs2_trans_begin(sdp, rblocks,
  1255. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  1256. if (error)
  1257. goto out_trans_fail;
  1258. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1259. gfs2_trans_end(sdp);
  1260. if (error)
  1261. goto out_trans_fail;
  1262. len -= max_bytes;
  1263. offset += max_bytes;
  1264. gfs2_inplace_release(ip);
  1265. gfs2_quota_unlock(ip);
  1266. gfs2_alloc_put(ip);
  1267. }
  1268. goto out_unlock;
  1269. out_trans_fail:
  1270. gfs2_inplace_release(ip);
  1271. out_qunlock:
  1272. gfs2_quota_unlock(ip);
  1273. out_alloc_put:
  1274. gfs2_alloc_put(ip);
  1275. out_unlock:
  1276. gfs2_glock_dq(&ip->i_gh);
  1277. out_uninit:
  1278. gfs2_holder_uninit(&ip->i_gh);
  1279. return error;
  1280. }
  1281. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1282. u64 start, u64 len)
  1283. {
  1284. struct gfs2_inode *ip = GFS2_I(inode);
  1285. struct gfs2_holder gh;
  1286. int ret;
  1287. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1288. if (ret)
  1289. return ret;
  1290. mutex_lock(&inode->i_mutex);
  1291. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1292. if (ret)
  1293. goto out;
  1294. if (gfs2_is_stuffed(ip)) {
  1295. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1296. u64 size = i_size_read(inode);
  1297. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1298. FIEMAP_EXTENT_DATA_INLINE;
  1299. phys += sizeof(struct gfs2_dinode);
  1300. phys += start;
  1301. if (start + len > size)
  1302. len = size - start;
  1303. if (start < size)
  1304. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1305. len, flags);
  1306. if (ret == 1)
  1307. ret = 0;
  1308. } else {
  1309. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1310. gfs2_block_map);
  1311. }
  1312. gfs2_glock_dq_uninit(&gh);
  1313. out:
  1314. mutex_unlock(&inode->i_mutex);
  1315. return ret;
  1316. }
  1317. const struct inode_operations gfs2_file_iops = {
  1318. .permission = gfs2_permission,
  1319. .setattr = gfs2_setattr,
  1320. .getattr = gfs2_getattr,
  1321. .setxattr = gfs2_setxattr,
  1322. .getxattr = gfs2_getxattr,
  1323. .listxattr = gfs2_listxattr,
  1324. .removexattr = gfs2_removexattr,
  1325. .fallocate = gfs2_fallocate,
  1326. .fiemap = gfs2_fiemap,
  1327. };
  1328. const struct inode_operations gfs2_dir_iops = {
  1329. .create = gfs2_create,
  1330. .lookup = gfs2_lookup,
  1331. .link = gfs2_link,
  1332. .unlink = gfs2_unlink,
  1333. .symlink = gfs2_symlink,
  1334. .mkdir = gfs2_mkdir,
  1335. .rmdir = gfs2_rmdir,
  1336. .mknod = gfs2_mknod,
  1337. .rename = gfs2_rename,
  1338. .permission = gfs2_permission,
  1339. .setattr = gfs2_setattr,
  1340. .getattr = gfs2_getattr,
  1341. .setxattr = gfs2_setxattr,
  1342. .getxattr = gfs2_getxattr,
  1343. .listxattr = gfs2_listxattr,
  1344. .removexattr = gfs2_removexattr,
  1345. .fiemap = gfs2_fiemap,
  1346. };
  1347. const struct inode_operations gfs2_symlink_iops = {
  1348. .readlink = generic_readlink,
  1349. .follow_link = gfs2_follow_link,
  1350. .put_link = gfs2_put_link,
  1351. .permission = gfs2_permission,
  1352. .setattr = gfs2_setattr,
  1353. .getattr = gfs2_getattr,
  1354. .setxattr = gfs2_setxattr,
  1355. .getxattr = gfs2_getxattr,
  1356. .listxattr = gfs2_listxattr,
  1357. .removexattr = gfs2_removexattr,
  1358. .fiemap = gfs2_fiemap,
  1359. };