ops_inode.c 26 KB

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