ops_inode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/lm_interface.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "eaops.h"
  28. #include "eattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "ops_dentry.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. /**
  39. * gfs2_create - Create a file
  40. * @dir: The directory in which to create the file
  41. * @dentry: The dentry of the new file
  42. * @mode: The mode of the new file
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  47. int mode, struct nameidata *nd)
  48. {
  49. struct gfs2_inode *dip = GFS2_I(dir);
  50. struct gfs2_sbd *sdp = GFS2_SB(dir);
  51. struct gfs2_holder ghs[2];
  52. struct inode *inode;
  53. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  54. for (;;) {
  55. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  56. if (!IS_ERR(inode)) {
  57. gfs2_trans_end(sdp);
  58. if (dip->i_alloc->al_rgd)
  59. gfs2_inplace_release(dip);
  60. gfs2_quota_unlock(dip);
  61. gfs2_alloc_put(dip);
  62. gfs2_glock_dq_uninit_m(2, ghs);
  63. mark_inode_dirty(inode);
  64. break;
  65. } else if (PTR_ERR(inode) != -EEXIST ||
  66. (nd && (nd->intent.open.flags & O_EXCL))) {
  67. gfs2_holder_uninit(ghs);
  68. return PTR_ERR(inode);
  69. }
  70. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  71. if (inode) {
  72. if (!IS_ERR(inode)) {
  73. gfs2_holder_uninit(ghs);
  74. break;
  75. } else {
  76. gfs2_holder_uninit(ghs);
  77. return PTR_ERR(inode);
  78. }
  79. }
  80. }
  81. d_instantiate(dentry, inode);
  82. return 0;
  83. }
  84. /**
  85. * gfs2_lookup - Look up a filename in a directory and return its inode
  86. * @dir: The directory inode
  87. * @dentry: The dentry of the new inode
  88. * @nd: passed from Linux VFS, ignored by us
  89. *
  90. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  91. *
  92. * Returns: errno
  93. */
  94. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  95. struct nameidata *nd)
  96. {
  97. struct inode *inode = NULL;
  98. dentry->d_op = &gfs2_dops;
  99. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  100. if (inode && IS_ERR(inode))
  101. return ERR_CAST(inode);
  102. if (inode) {
  103. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  104. struct gfs2_holder gh;
  105. int error;
  106. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  107. if (error) {
  108. iput(inode);
  109. return ERR_PTR(error);
  110. }
  111. gfs2_glock_dq_uninit(&gh);
  112. return d_splice_alias(inode, dentry);
  113. }
  114. d_add(dentry, inode);
  115. return NULL;
  116. }
  117. /**
  118. * gfs2_link - Link to a file
  119. * @old_dentry: The inode to link
  120. * @dir: Add link to this directory
  121. * @dentry: The name of the link
  122. *
  123. * Link the inode in "old_dentry" into the directory "dir" with the
  124. * name in "dentry".
  125. *
  126. * Returns: errno
  127. */
  128. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  129. struct dentry *dentry)
  130. {
  131. struct gfs2_inode *dip = GFS2_I(dir);
  132. struct gfs2_sbd *sdp = GFS2_SB(dir);
  133. struct inode *inode = old_dentry->d_inode;
  134. struct gfs2_inode *ip = GFS2_I(inode);
  135. struct gfs2_holder ghs[2];
  136. int alloc_required;
  137. int error;
  138. if (S_ISDIR(inode->i_mode))
  139. return -EPERM;
  140. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  141. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  142. error = gfs2_glock_nq(ghs); /* parent */
  143. if (error)
  144. goto out_parent;
  145. error = gfs2_glock_nq(ghs + 1); /* child */
  146. if (error)
  147. goto out_child;
  148. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  149. if (error)
  150. goto out_gunlock;
  151. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  152. switch (error) {
  153. case -ENOENT:
  154. break;
  155. case 0:
  156. error = -EEXIST;
  157. default:
  158. goto out_gunlock;
  159. }
  160. error = -EINVAL;
  161. if (!dip->i_inode.i_nlink)
  162. goto out_gunlock;
  163. error = -EFBIG;
  164. if (dip->i_di.di_entries == (u32)-1)
  165. goto out_gunlock;
  166. error = -EPERM;
  167. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  168. goto out_gunlock;
  169. error = -EINVAL;
  170. if (!ip->i_inode.i_nlink)
  171. goto out_gunlock;
  172. error = -EMLINK;
  173. if (ip->i_inode.i_nlink == (u32)-1)
  174. goto out_gunlock;
  175. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  176. if (error < 0)
  177. goto out_gunlock;
  178. error = 0;
  179. if (alloc_required) {
  180. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  181. if (!al) {
  182. error = -ENOMEM;
  183. goto out_gunlock;
  184. }
  185. error = gfs2_quota_lock_check(dip);
  186. if (error)
  187. goto out_alloc;
  188. al->al_requested = sdp->sd_max_dirres;
  189. error = gfs2_inplace_reserve(dip);
  190. if (error)
  191. goto out_gunlock_q;
  192. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  193. al->al_rgd->rd_length +
  194. 2 * RES_DINODE + RES_STATFS +
  195. RES_QUOTA, 0);
  196. if (error)
  197. goto out_ipres;
  198. } else {
  199. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  200. if (error)
  201. goto out_ipres;
  202. }
  203. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  204. if (error)
  205. goto out_end_trans;
  206. error = gfs2_change_nlink(ip, +1);
  207. out_end_trans:
  208. gfs2_trans_end(sdp);
  209. out_ipres:
  210. if (alloc_required)
  211. gfs2_inplace_release(dip);
  212. out_gunlock_q:
  213. if (alloc_required)
  214. gfs2_quota_unlock(dip);
  215. out_alloc:
  216. if (alloc_required)
  217. gfs2_alloc_put(dip);
  218. out_gunlock:
  219. gfs2_glock_dq(ghs + 1);
  220. out_child:
  221. gfs2_glock_dq(ghs);
  222. out_parent:
  223. gfs2_holder_uninit(ghs);
  224. gfs2_holder_uninit(ghs + 1);
  225. if (!error) {
  226. atomic_inc(&inode->i_count);
  227. d_instantiate(dentry, inode);
  228. mark_inode_dirty(inode);
  229. }
  230. return error;
  231. }
  232. /**
  233. * gfs2_unlink - Unlink a file
  234. * @dir: The inode of the directory containing the file to unlink
  235. * @dentry: The file itself
  236. *
  237. * Unlink a file. Call gfs2_unlinki()
  238. *
  239. * Returns: errno
  240. */
  241. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  242. {
  243. struct gfs2_inode *dip = GFS2_I(dir);
  244. struct gfs2_sbd *sdp = GFS2_SB(dir);
  245. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  246. struct gfs2_holder ghs[3];
  247. struct gfs2_rgrpd *rgd;
  248. struct gfs2_holder ri_gh;
  249. int error;
  250. error = gfs2_rindex_hold(sdp, &ri_gh);
  251. if (error)
  252. return error;
  253. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  254. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  255. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  256. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  257. error = gfs2_glock_nq(ghs); /* parent */
  258. if (error)
  259. goto out_parent;
  260. error = gfs2_glock_nq(ghs + 1); /* child */
  261. if (error)
  262. goto out_child;
  263. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  264. if (error)
  265. goto out_rgrp;
  266. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  267. if (error)
  268. goto out_gunlock;
  269. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  270. if (error)
  271. goto out_rgrp;
  272. error = gfs2_dir_del(dip, &dentry->d_name);
  273. if (error)
  274. goto out_end_trans;
  275. error = gfs2_change_nlink(ip, -1);
  276. out_end_trans:
  277. gfs2_trans_end(sdp);
  278. out_gunlock:
  279. gfs2_glock_dq(ghs + 2);
  280. out_rgrp:
  281. gfs2_holder_uninit(ghs + 2);
  282. gfs2_glock_dq(ghs + 1);
  283. out_child:
  284. gfs2_holder_uninit(ghs + 1);
  285. gfs2_glock_dq(ghs);
  286. out_parent:
  287. gfs2_holder_uninit(ghs);
  288. gfs2_glock_dq_uninit(&ri_gh);
  289. return error;
  290. }
  291. /**
  292. * gfs2_symlink - Create a symlink
  293. * @dir: The directory to create the symlink in
  294. * @dentry: The dentry to put the symlink in
  295. * @symname: The thing which the link points to
  296. *
  297. * Returns: errno
  298. */
  299. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  300. const char *symname)
  301. {
  302. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  303. struct gfs2_sbd *sdp = GFS2_SB(dir);
  304. struct gfs2_holder ghs[2];
  305. struct inode *inode;
  306. struct buffer_head *dibh;
  307. int size;
  308. int error;
  309. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  310. size = strlen(symname);
  311. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  312. return -ENAMETOOLONG;
  313. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  314. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  315. if (IS_ERR(inode)) {
  316. gfs2_holder_uninit(ghs);
  317. return PTR_ERR(inode);
  318. }
  319. ip = ghs[1].gh_gl->gl_object;
  320. ip->i_di.di_size = size;
  321. error = gfs2_meta_inode_buffer(ip, &dibh);
  322. if (!gfs2_assert_withdraw(sdp, !error)) {
  323. gfs2_dinode_out(ip, dibh->b_data);
  324. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  325. size);
  326. brelse(dibh);
  327. }
  328. gfs2_trans_end(sdp);
  329. if (dip->i_alloc->al_rgd)
  330. gfs2_inplace_release(dip);
  331. gfs2_quota_unlock(dip);
  332. gfs2_alloc_put(dip);
  333. gfs2_glock_dq_uninit_m(2, ghs);
  334. d_instantiate(dentry, inode);
  335. mark_inode_dirty(inode);
  336. return 0;
  337. }
  338. /**
  339. * gfs2_mkdir - Make a directory
  340. * @dir: The parent directory of the new one
  341. * @dentry: The dentry of the new directory
  342. * @mode: The mode of the new directory
  343. *
  344. * Returns: errno
  345. */
  346. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  347. {
  348. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  349. struct gfs2_sbd *sdp = GFS2_SB(dir);
  350. struct gfs2_holder ghs[2];
  351. struct inode *inode;
  352. struct buffer_head *dibh;
  353. int error;
  354. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  355. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  356. if (IS_ERR(inode)) {
  357. gfs2_holder_uninit(ghs);
  358. return PTR_ERR(inode);
  359. }
  360. ip = ghs[1].gh_gl->gl_object;
  361. ip->i_inode.i_nlink = 2;
  362. ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  363. ip->i_di.di_flags |= GFS2_DIF_JDATA;
  364. ip->i_di.di_entries = 2;
  365. error = gfs2_meta_inode_buffer(ip, &dibh);
  366. if (!gfs2_assert_withdraw(sdp, !error)) {
  367. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  368. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  369. struct qstr str;
  370. gfs2_str2qstr(&str, ".");
  371. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  372. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  373. dent->de_inum = di->di_num; /* already GFS2 endian */
  374. dent->de_type = cpu_to_be16(DT_DIR);
  375. di->di_entries = cpu_to_be32(1);
  376. gfs2_str2qstr(&str, "..");
  377. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  378. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  379. gfs2_inum_out(dip, dent);
  380. dent->de_type = cpu_to_be16(DT_DIR);
  381. gfs2_dinode_out(ip, di);
  382. brelse(dibh);
  383. }
  384. error = gfs2_change_nlink(dip, +1);
  385. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  386. gfs2_trans_end(sdp);
  387. if (dip->i_alloc->al_rgd)
  388. gfs2_inplace_release(dip);
  389. gfs2_quota_unlock(dip);
  390. gfs2_alloc_put(dip);
  391. gfs2_glock_dq_uninit_m(2, ghs);
  392. d_instantiate(dentry, inode);
  393. mark_inode_dirty(inode);
  394. return 0;
  395. }
  396. /**
  397. * gfs2_rmdir - Remove a directory
  398. * @dir: The parent directory of the directory to be removed
  399. * @dentry: The dentry of the directory to remove
  400. *
  401. * Remove a directory. Call gfs2_rmdiri()
  402. *
  403. * Returns: errno
  404. */
  405. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  406. {
  407. struct gfs2_inode *dip = GFS2_I(dir);
  408. struct gfs2_sbd *sdp = GFS2_SB(dir);
  409. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  410. struct gfs2_holder ghs[3];
  411. struct gfs2_rgrpd *rgd;
  412. struct gfs2_holder ri_gh;
  413. int error;
  414. error = gfs2_rindex_hold(sdp, &ri_gh);
  415. if (error)
  416. return error;
  417. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  418. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  419. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  420. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  421. error = gfs2_glock_nq(ghs); /* parent */
  422. if (error)
  423. goto out_parent;
  424. error = gfs2_glock_nq(ghs + 1); /* child */
  425. if (error)
  426. goto out_child;
  427. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  428. if (error)
  429. goto out_rgrp;
  430. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  431. if (error)
  432. goto out_gunlock;
  433. if (ip->i_di.di_entries < 2) {
  434. if (gfs2_consist_inode(ip))
  435. gfs2_dinode_print(ip);
  436. error = -EIO;
  437. goto out_gunlock;
  438. }
  439. if (ip->i_di.di_entries > 2) {
  440. error = -ENOTEMPTY;
  441. goto out_gunlock;
  442. }
  443. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  444. if (error)
  445. goto out_gunlock;
  446. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  447. gfs2_trans_end(sdp);
  448. out_gunlock:
  449. gfs2_glock_dq(ghs + 2);
  450. out_rgrp:
  451. gfs2_holder_uninit(ghs + 2);
  452. gfs2_glock_dq(ghs + 1);
  453. out_child:
  454. gfs2_holder_uninit(ghs + 1);
  455. gfs2_glock_dq(ghs);
  456. out_parent:
  457. gfs2_holder_uninit(ghs);
  458. gfs2_glock_dq_uninit(&ri_gh);
  459. return error;
  460. }
  461. /**
  462. * gfs2_mknod - Make a special file
  463. * @dir: The directory in which the special file will reside
  464. * @dentry: The dentry of the special file
  465. * @mode: The mode of the special file
  466. * @rdev: The device specification of the special file
  467. *
  468. */
  469. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  470. dev_t dev)
  471. {
  472. struct gfs2_inode *dip = GFS2_I(dir);
  473. struct gfs2_sbd *sdp = GFS2_SB(dir);
  474. struct gfs2_holder ghs[2];
  475. struct inode *inode;
  476. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  477. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  478. if (IS_ERR(inode)) {
  479. gfs2_holder_uninit(ghs);
  480. return PTR_ERR(inode);
  481. }
  482. gfs2_trans_end(sdp);
  483. if (dip->i_alloc->al_rgd)
  484. gfs2_inplace_release(dip);
  485. gfs2_quota_unlock(dip);
  486. gfs2_alloc_put(dip);
  487. gfs2_glock_dq_uninit_m(2, ghs);
  488. d_instantiate(dentry, inode);
  489. mark_inode_dirty(inode);
  490. return 0;
  491. }
  492. /**
  493. * gfs2_rename - Rename a file
  494. * @odir: Parent directory of old file name
  495. * @odentry: The old dentry of the file
  496. * @ndir: Parent directory of new file name
  497. * @ndentry: The new dentry of the file
  498. *
  499. * Returns: errno
  500. */
  501. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  502. struct inode *ndir, struct dentry *ndentry)
  503. {
  504. struct gfs2_inode *odip = GFS2_I(odir);
  505. struct gfs2_inode *ndip = GFS2_I(ndir);
  506. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  507. struct gfs2_inode *nip = NULL;
  508. struct gfs2_sbd *sdp = GFS2_SB(odir);
  509. struct gfs2_holder ghs[5], r_gh;
  510. struct gfs2_rgrpd *nrgd;
  511. unsigned int num_gh;
  512. int dir_rename = 0;
  513. int alloc_required;
  514. unsigned int x;
  515. int error;
  516. if (ndentry->d_inode) {
  517. nip = GFS2_I(ndentry->d_inode);
  518. if (ip == nip)
  519. return 0;
  520. }
  521. /* Make sure we aren't trying to move a dirctory into it's subdir */
  522. if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
  523. dir_rename = 1;
  524. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
  525. &r_gh);
  526. if (error)
  527. goto out;
  528. error = gfs2_ok_to_move(ip, ndip);
  529. if (error)
  530. goto out_gunlock_r;
  531. }
  532. num_gh = 1;
  533. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  534. if (odip != ndip) {
  535. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  536. num_gh++;
  537. }
  538. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  539. num_gh++;
  540. if (nip) {
  541. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  542. num_gh++;
  543. /* grab the resource lock for unlink flag twiddling
  544. * this is the case of the target file already existing
  545. * so we unlink before doing the rename
  546. */
  547. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  548. if (nrgd)
  549. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  550. }
  551. for (x = 0; x < num_gh; x++) {
  552. error = gfs2_glock_nq(ghs + x);
  553. if (error)
  554. goto out_gunlock;
  555. }
  556. /* Check out the old directory */
  557. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  558. if (error)
  559. goto out_gunlock;
  560. /* Check out the new directory */
  561. if (nip) {
  562. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  563. if (error)
  564. goto out_gunlock;
  565. if (S_ISDIR(nip->i_inode.i_mode)) {
  566. if (nip->i_di.di_entries < 2) {
  567. if (gfs2_consist_inode(nip))
  568. gfs2_dinode_print(nip);
  569. error = -EIO;
  570. goto out_gunlock;
  571. }
  572. if (nip->i_di.di_entries > 2) {
  573. error = -ENOTEMPTY;
  574. goto out_gunlock;
  575. }
  576. }
  577. } else {
  578. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  579. if (error)
  580. goto out_gunlock;
  581. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  582. switch (error) {
  583. case -ENOENT:
  584. error = 0;
  585. break;
  586. case 0:
  587. error = -EEXIST;
  588. default:
  589. goto out_gunlock;
  590. };
  591. if (odip != ndip) {
  592. if (!ndip->i_inode.i_nlink) {
  593. error = -EINVAL;
  594. goto out_gunlock;
  595. }
  596. if (ndip->i_di.di_entries == (u32)-1) {
  597. error = -EFBIG;
  598. goto out_gunlock;
  599. }
  600. if (S_ISDIR(ip->i_inode.i_mode) &&
  601. ndip->i_inode.i_nlink == (u32)-1) {
  602. error = -EMLINK;
  603. goto out_gunlock;
  604. }
  605. }
  606. }
  607. /* Check out the dir to be renamed */
  608. if (dir_rename) {
  609. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  610. if (error)
  611. goto out_gunlock;
  612. }
  613. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  614. if (error < 0)
  615. goto out_gunlock;
  616. error = 0;
  617. if (alloc_required) {
  618. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  619. if (!al) {
  620. error = -ENOMEM;
  621. goto out_gunlock;
  622. }
  623. error = gfs2_quota_lock_check(ndip);
  624. if (error)
  625. goto out_alloc;
  626. al->al_requested = sdp->sd_max_dirres;
  627. error = gfs2_inplace_reserve(ndip);
  628. if (error)
  629. goto out_gunlock_q;
  630. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  631. al->al_rgd->rd_length +
  632. 4 * RES_DINODE + 4 * RES_LEAF +
  633. RES_STATFS + RES_QUOTA + 4, 0);
  634. if (error)
  635. goto out_ipreserv;
  636. } else {
  637. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  638. 5 * RES_LEAF + 4, 0);
  639. if (error)
  640. goto out_gunlock;
  641. }
  642. /* Remove the target file, if it exists */
  643. if (nip) {
  644. if (S_ISDIR(nip->i_inode.i_mode))
  645. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  646. else {
  647. error = gfs2_dir_del(ndip, &ndentry->d_name);
  648. if (error)
  649. goto out_end_trans;
  650. error = gfs2_change_nlink(nip, -1);
  651. }
  652. if (error)
  653. goto out_end_trans;
  654. }
  655. if (dir_rename) {
  656. struct qstr name;
  657. gfs2_str2qstr(&name, "..");
  658. error = gfs2_change_nlink(ndip, +1);
  659. if (error)
  660. goto out_end_trans;
  661. error = gfs2_change_nlink(odip, -1);
  662. if (error)
  663. goto out_end_trans;
  664. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  665. if (error)
  666. goto out_end_trans;
  667. } else {
  668. struct buffer_head *dibh;
  669. error = gfs2_meta_inode_buffer(ip, &dibh);
  670. if (error)
  671. goto out_end_trans;
  672. ip->i_inode.i_ctime = CURRENT_TIME;
  673. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  674. gfs2_dinode_out(ip, dibh->b_data);
  675. brelse(dibh);
  676. }
  677. error = gfs2_dir_del(odip, &odentry->d_name);
  678. if (error)
  679. goto out_end_trans;
  680. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  681. if (error)
  682. goto out_end_trans;
  683. out_end_trans:
  684. gfs2_trans_end(sdp);
  685. out_ipreserv:
  686. if (alloc_required)
  687. gfs2_inplace_release(ndip);
  688. out_gunlock_q:
  689. if (alloc_required)
  690. gfs2_quota_unlock(ndip);
  691. out_alloc:
  692. if (alloc_required)
  693. gfs2_alloc_put(ndip);
  694. out_gunlock:
  695. while (x--) {
  696. gfs2_glock_dq(ghs + x);
  697. gfs2_holder_uninit(ghs + x);
  698. }
  699. out_gunlock_r:
  700. if (dir_rename)
  701. gfs2_glock_dq_uninit(&r_gh);
  702. out:
  703. return error;
  704. }
  705. /**
  706. * gfs2_readlink - Read the value of a symlink
  707. * @dentry: the symlink
  708. * @buf: the buffer to read the symlink data into
  709. * @size: the size of the buffer
  710. *
  711. * Returns: errno
  712. */
  713. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  714. int user_size)
  715. {
  716. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  717. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  718. unsigned int len = GFS2_FAST_NAME_SIZE;
  719. int error;
  720. error = gfs2_readlinki(ip, &buf, &len);
  721. if (error)
  722. return error;
  723. if (user_size > len - 1)
  724. user_size = len - 1;
  725. if (copy_to_user(user_buf, buf, user_size))
  726. error = -EFAULT;
  727. else
  728. error = user_size;
  729. if (buf != array)
  730. kfree(buf);
  731. return error;
  732. }
  733. /**
  734. * gfs2_follow_link - Follow a symbolic link
  735. * @dentry: The dentry of the link
  736. * @nd: Data that we pass to vfs_follow_link()
  737. *
  738. * This can handle symlinks of any size. It is optimised for symlinks
  739. * under GFS2_FAST_NAME_SIZE.
  740. *
  741. * Returns: 0 on success or error code
  742. */
  743. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  744. {
  745. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  746. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  747. unsigned int len = GFS2_FAST_NAME_SIZE;
  748. int error;
  749. error = gfs2_readlinki(ip, &buf, &len);
  750. if (!error) {
  751. error = vfs_follow_link(nd, buf);
  752. if (buf != array)
  753. kfree(buf);
  754. }
  755. return ERR_PTR(error);
  756. }
  757. /**
  758. * gfs2_permission -
  759. * @inode:
  760. * @mask:
  761. * @nd: passed from Linux VFS, ignored by us
  762. *
  763. * This may be called from the VFS directly, or from within GFS2 with the
  764. * inode locked, so we look to see if the glock is already locked and only
  765. * lock the glock if its not already been done.
  766. *
  767. * Returns: errno
  768. */
  769. int gfs2_permission(struct inode *inode, int mask)
  770. {
  771. struct gfs2_inode *ip = GFS2_I(inode);
  772. struct gfs2_holder i_gh;
  773. int error;
  774. int unlock = 0;
  775. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  776. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  777. if (error)
  778. return error;
  779. unlock = 1;
  780. }
  781. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  782. error = -EACCES;
  783. else
  784. error = generic_permission(inode, mask, gfs2_check_acl);
  785. if (unlock)
  786. gfs2_glock_dq_uninit(&i_gh);
  787. return error;
  788. }
  789. static int setattr_size(struct inode *inode, struct iattr *attr)
  790. {
  791. struct gfs2_inode *ip = GFS2_I(inode);
  792. struct gfs2_sbd *sdp = GFS2_SB(inode);
  793. int error;
  794. if (attr->ia_size != ip->i_di.di_size) {
  795. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  796. if (error)
  797. return error;
  798. error = vmtruncate(inode, attr->ia_size);
  799. gfs2_trans_end(sdp);
  800. if (error)
  801. return error;
  802. }
  803. error = gfs2_truncatei(ip, attr->ia_size);
  804. if (error && (inode->i_size != ip->i_di.di_size))
  805. i_size_write(inode, ip->i_di.di_size);
  806. return error;
  807. }
  808. static int setattr_chown(struct inode *inode, struct iattr *attr)
  809. {
  810. struct gfs2_inode *ip = GFS2_I(inode);
  811. struct gfs2_sbd *sdp = GFS2_SB(inode);
  812. struct buffer_head *dibh;
  813. u32 ouid, ogid, nuid, ngid;
  814. int error;
  815. ouid = inode->i_uid;
  816. ogid = inode->i_gid;
  817. nuid = attr->ia_uid;
  818. ngid = attr->ia_gid;
  819. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  820. ouid = nuid = NO_QUOTA_CHANGE;
  821. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  822. ogid = ngid = NO_QUOTA_CHANGE;
  823. if (!gfs2_alloc_get(ip))
  824. return -ENOMEM;
  825. error = gfs2_quota_lock(ip, nuid, ngid);
  826. if (error)
  827. goto out_alloc;
  828. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  829. error = gfs2_quota_check(ip, nuid, ngid);
  830. if (error)
  831. goto out_gunlock_q;
  832. }
  833. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  834. if (error)
  835. goto out_gunlock_q;
  836. error = gfs2_meta_inode_buffer(ip, &dibh);
  837. if (error)
  838. goto out_end_trans;
  839. error = inode_setattr(inode, attr);
  840. gfs2_assert_warn(sdp, !error);
  841. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  842. gfs2_dinode_out(ip, dibh->b_data);
  843. brelse(dibh);
  844. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  845. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  846. gfs2_quota_change(ip, -blocks, ouid, ogid);
  847. gfs2_quota_change(ip, blocks, nuid, ngid);
  848. }
  849. out_end_trans:
  850. gfs2_trans_end(sdp);
  851. out_gunlock_q:
  852. gfs2_quota_unlock(ip);
  853. out_alloc:
  854. gfs2_alloc_put(ip);
  855. return error;
  856. }
  857. /**
  858. * gfs2_setattr - Change attributes on an inode
  859. * @dentry: The dentry which is changing
  860. * @attr: The structure describing the change
  861. *
  862. * The VFS layer wants to change one or more of an inodes attributes. Write
  863. * that change out to disk.
  864. *
  865. * Returns: errno
  866. */
  867. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  868. {
  869. struct inode *inode = dentry->d_inode;
  870. struct gfs2_inode *ip = GFS2_I(inode);
  871. struct gfs2_holder i_gh;
  872. int error;
  873. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  874. if (error)
  875. return error;
  876. error = -EPERM;
  877. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  878. goto out;
  879. error = inode_change_ok(inode, attr);
  880. if (error)
  881. goto out;
  882. if (attr->ia_valid & ATTR_SIZE)
  883. error = setattr_size(inode, attr);
  884. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  885. error = setattr_chown(inode, attr);
  886. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  887. error = gfs2_acl_chmod(ip, attr);
  888. else
  889. error = gfs2_setattr_simple(ip, attr);
  890. out:
  891. gfs2_glock_dq_uninit(&i_gh);
  892. if (!error)
  893. mark_inode_dirty(inode);
  894. return error;
  895. }
  896. /**
  897. * gfs2_getattr - Read out an inode's attributes
  898. * @mnt: The vfsmount the inode is being accessed from
  899. * @dentry: The dentry to stat
  900. * @stat: The inode's stats
  901. *
  902. * This may be called from the VFS directly, or from within GFS2 with the
  903. * inode locked, so we look to see if the glock is already locked and only
  904. * lock the glock if its not already been done. Note that its the NFS
  905. * readdirplus operation which causes this to be called (from filldir)
  906. * with the glock already held.
  907. *
  908. * Returns: errno
  909. */
  910. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  911. struct kstat *stat)
  912. {
  913. struct inode *inode = dentry->d_inode;
  914. struct gfs2_inode *ip = GFS2_I(inode);
  915. struct gfs2_holder gh;
  916. int error;
  917. int unlock = 0;
  918. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  919. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  920. if (error)
  921. return error;
  922. unlock = 1;
  923. }
  924. generic_fillattr(inode, stat);
  925. if (unlock)
  926. gfs2_glock_dq_uninit(&gh);
  927. return 0;
  928. }
  929. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  930. const void *data, size_t size, int flags)
  931. {
  932. struct inode *inode = dentry->d_inode;
  933. struct gfs2_ea_request er;
  934. memset(&er, 0, sizeof(struct gfs2_ea_request));
  935. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  936. if (er.er_type == GFS2_EATYPE_UNUSED)
  937. return -EOPNOTSUPP;
  938. er.er_data = (char *)data;
  939. er.er_name_len = strlen(er.er_name);
  940. er.er_data_len = size;
  941. er.er_flags = flags;
  942. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  943. return gfs2_ea_set(GFS2_I(inode), &er);
  944. }
  945. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  946. void *data, size_t size)
  947. {
  948. struct gfs2_ea_request er;
  949. memset(&er, 0, sizeof(struct gfs2_ea_request));
  950. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  951. if (er.er_type == GFS2_EATYPE_UNUSED)
  952. return -EOPNOTSUPP;
  953. er.er_data = data;
  954. er.er_name_len = strlen(er.er_name);
  955. er.er_data_len = size;
  956. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  957. }
  958. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  959. {
  960. struct gfs2_ea_request er;
  961. memset(&er, 0, sizeof(struct gfs2_ea_request));
  962. er.er_data = (size) ? buffer : NULL;
  963. er.er_data_len = size;
  964. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  965. }
  966. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  967. {
  968. struct gfs2_ea_request er;
  969. memset(&er, 0, sizeof(struct gfs2_ea_request));
  970. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  971. if (er.er_type == GFS2_EATYPE_UNUSED)
  972. return -EOPNOTSUPP;
  973. er.er_name_len = strlen(er.er_name);
  974. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  975. }
  976. const struct inode_operations gfs2_file_iops = {
  977. .permission = gfs2_permission,
  978. .setattr = gfs2_setattr,
  979. .getattr = gfs2_getattr,
  980. .setxattr = gfs2_setxattr,
  981. .getxattr = gfs2_getxattr,
  982. .listxattr = gfs2_listxattr,
  983. .removexattr = gfs2_removexattr,
  984. };
  985. const struct inode_operations gfs2_dir_iops = {
  986. .create = gfs2_create,
  987. .lookup = gfs2_lookup,
  988. .link = gfs2_link,
  989. .unlink = gfs2_unlink,
  990. .symlink = gfs2_symlink,
  991. .mkdir = gfs2_mkdir,
  992. .rmdir = gfs2_rmdir,
  993. .mknod = gfs2_mknod,
  994. .rename = gfs2_rename,
  995. .permission = gfs2_permission,
  996. .setattr = gfs2_setattr,
  997. .getattr = gfs2_getattr,
  998. .setxattr = gfs2_setxattr,
  999. .getxattr = gfs2_getxattr,
  1000. .listxattr = gfs2_listxattr,
  1001. .removexattr = gfs2_removexattr,
  1002. };
  1003. const struct inode_operations gfs2_symlink_iops = {
  1004. .readlink = gfs2_readlink,
  1005. .follow_link = gfs2_follow_link,
  1006. .permission = gfs2_permission,
  1007. .setattr = gfs2_setattr,
  1008. .getattr = gfs2_getattr,
  1009. .setxattr = gfs2_setxattr,
  1010. .getxattr = gfs2_getxattr,
  1011. .listxattr = gfs2_listxattr,
  1012. .removexattr = gfs2_removexattr,
  1013. };