inode.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/security.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 "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. unsigned int mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  315. unsigned int *uid, unsigned int *gid)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(*mode))
  320. *mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. *mode &= ~07111;
  323. *uid = dip->i_inode.i_uid;
  324. } else
  325. *uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(*mode))
  328. *mode |= S_ISGID;
  329. *gid = dip->i_inode.i_gid;
  330. } else
  331. *gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  336. int error;
  337. if (gfs2_alloc_get(dip) == NULL)
  338. return -ENOMEM;
  339. dip->i_alloc->al_requested = RES_DINODE;
  340. error = gfs2_inplace_reserve(dip);
  341. if (error)
  342. goto out;
  343. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  344. if (error)
  345. goto out_ipreserv;
  346. error = gfs2_alloc_di(dip, no_addr, generation);
  347. gfs2_trans_end(sdp);
  348. out_ipreserv:
  349. gfs2_inplace_release(dip);
  350. out:
  351. gfs2_alloc_put(dip);
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @gl: The glock covering the new inode
  371. * @inum: The inode number
  372. * @mode: The file permissions
  373. * @uid: The uid of the new inode
  374. * @gid: The gid of the new inode
  375. * @generation: The generation number of the new inode
  376. * @dev: The device number (if a device node)
  377. * @symname: The symlink destination (if a symlink)
  378. * @size: The inode size (ignored for directories)
  379. * @bhp: The buffer head (returned to caller)
  380. *
  381. */
  382. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  383. const struct gfs2_inum_host *inum, unsigned int mode,
  384. unsigned int uid, unsigned int gid,
  385. const u64 *generation, dev_t dev, const char *symname,
  386. unsigned size, struct buffer_head **bhp)
  387. {
  388. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  389. struct gfs2_dinode *di;
  390. struct buffer_head *dibh;
  391. struct timespec tv = CURRENT_TIME;
  392. dibh = gfs2_meta_new(gl, inum->no_addr);
  393. gfs2_trans_add_bh(gl, dibh, 1);
  394. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  395. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  396. di = (struct gfs2_dinode *)dibh->b_data;
  397. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  398. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  399. di->di_mode = cpu_to_be32(mode);
  400. di->di_uid = cpu_to_be32(uid);
  401. di->di_gid = cpu_to_be32(gid);
  402. di->di_nlink = 0;
  403. di->di_size = cpu_to_be64(size);
  404. di->di_blocks = cpu_to_be64(1);
  405. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  406. di->di_major = cpu_to_be32(MAJOR(dev));
  407. di->di_minor = cpu_to_be32(MINOR(dev));
  408. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  409. di->di_generation = cpu_to_be64(*generation);
  410. di->di_flags = 0;
  411. di->__pad1 = 0;
  412. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  413. di->di_height = 0;
  414. di->__pad2 = 0;
  415. di->__pad3 = 0;
  416. di->di_depth = 0;
  417. di->di_entries = 0;
  418. memset(&di->__pad4, 0, sizeof(di->__pad4));
  419. di->di_eattr = 0;
  420. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  421. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  422. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  423. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  424. switch(mode & S_IFMT) {
  425. case S_IFREG:
  426. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  427. gfs2_tune_get(sdp, gt_new_files_jdata))
  428. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  429. break;
  430. case S_IFDIR:
  431. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  432. GFS2_DIF_INHERIT_JDATA);
  433. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  434. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  435. di->di_entries = cpu_to_be32(2);
  436. gfs2_init_dir(dibh, dip);
  437. break;
  438. case S_IFLNK:
  439. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
  440. break;
  441. }
  442. set_buffer_uptodate(dibh);
  443. *bhp = dibh;
  444. }
  445. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  446. unsigned int mode, const struct gfs2_inum_host *inum,
  447. const u64 *generation, dev_t dev, const char *symname,
  448. unsigned int size, struct buffer_head **bhp)
  449. {
  450. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  451. unsigned int uid, gid;
  452. int error;
  453. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  454. if (!gfs2_alloc_get(dip))
  455. return -ENOMEM;
  456. error = gfs2_quota_lock(dip, uid, gid);
  457. if (error)
  458. goto out;
  459. error = gfs2_quota_check(dip, uid, gid);
  460. if (error)
  461. goto out_quota;
  462. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  463. if (error)
  464. goto out_quota;
  465. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
  466. gfs2_quota_change(dip, +1, uid, gid);
  467. gfs2_trans_end(sdp);
  468. out_quota:
  469. gfs2_quota_unlock(dip);
  470. out:
  471. gfs2_alloc_put(dip);
  472. return error;
  473. }
  474. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  475. struct gfs2_inode *ip)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  478. struct gfs2_alloc *al;
  479. int alloc_required;
  480. struct buffer_head *dibh;
  481. int error;
  482. al = gfs2_alloc_get(dip);
  483. if (!al)
  484. return -ENOMEM;
  485. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  486. if (error)
  487. goto fail;
  488. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  489. if (alloc_required < 0)
  490. goto fail_quota_locks;
  491. if (alloc_required) {
  492. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  493. if (error)
  494. goto fail_quota_locks;
  495. al->al_requested = sdp->sd_max_dirres;
  496. error = gfs2_inplace_reserve(dip);
  497. if (error)
  498. goto fail_quota_locks;
  499. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  500. al->al_rgd->rd_length +
  501. 2 * RES_DINODE +
  502. RES_STATFS + RES_QUOTA, 0);
  503. if (error)
  504. goto fail_ipreserv;
  505. } else {
  506. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  507. if (error)
  508. goto fail_quota_locks;
  509. }
  510. error = gfs2_dir_add(&dip->i_inode, name, ip);
  511. if (error)
  512. goto fail_end_trans;
  513. error = gfs2_meta_inode_buffer(ip, &dibh);
  514. if (error)
  515. goto fail_end_trans;
  516. inc_nlink(&ip->i_inode);
  517. if (S_ISDIR(ip->i_inode.i_mode))
  518. inc_nlink(&ip->i_inode);
  519. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  520. gfs2_dinode_out(ip, dibh->b_data);
  521. brelse(dibh);
  522. return 0;
  523. fail_end_trans:
  524. gfs2_trans_end(sdp);
  525. fail_ipreserv:
  526. if (dip->i_alloc->al_rgd)
  527. gfs2_inplace_release(dip);
  528. fail_quota_locks:
  529. gfs2_quota_unlock(dip);
  530. fail:
  531. gfs2_alloc_put(dip);
  532. return error;
  533. }
  534. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  535. const struct qstr *qstr)
  536. {
  537. int err;
  538. size_t len;
  539. void *value;
  540. char *name;
  541. err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  542. &name, &value, &len);
  543. if (err) {
  544. if (err == -EOPNOTSUPP)
  545. return 0;
  546. return err;
  547. }
  548. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  549. GFS2_EATYPE_SECURITY);
  550. kfree(value);
  551. kfree(name);
  552. return err;
  553. }
  554. /**
  555. * gfs2_create_inode - Create a new inode
  556. * @dir: The parent directory
  557. * @dentry: The new dentry
  558. * @mode: The permissions on the new inode
  559. * @dev: For device nodes, this is the device number
  560. * @symname: For symlinks, this is the link destination
  561. * @size: The initial size of the inode (ignored for directories)
  562. *
  563. * Returns: 0 on success, or error code
  564. */
  565. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  566. unsigned int mode, dev_t dev, const char *symname,
  567. unsigned int size)
  568. {
  569. const struct qstr *name = &dentry->d_name;
  570. struct gfs2_holder ghs[2];
  571. struct inode *inode = NULL;
  572. struct gfs2_inode *dip = GFS2_I(dir);
  573. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  574. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  575. int error;
  576. u64 generation;
  577. struct buffer_head *bh = NULL;
  578. if (!name->len || name->len > GFS2_FNAMESIZE)
  579. return -ENAMETOOLONG;
  580. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  581. if (error)
  582. goto fail;
  583. error = create_ok(dip, name, mode);
  584. if (error)
  585. goto fail_gunlock;
  586. error = alloc_dinode(dip, &inum.no_addr, &generation);
  587. if (error)
  588. goto fail_gunlock;
  589. inum.no_formal_ino = generation;
  590. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  591. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  592. if (error)
  593. goto fail_gunlock;
  594. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
  595. if (error)
  596. goto fail_gunlock2;
  597. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  598. inum.no_formal_ino, 0);
  599. if (IS_ERR(inode))
  600. goto fail_gunlock2;
  601. error = gfs2_inode_refresh(GFS2_I(inode));
  602. if (error)
  603. goto fail_gunlock2;
  604. error = gfs2_acl_create(dip, inode);
  605. if (error)
  606. goto fail_gunlock2;
  607. error = gfs2_security_init(dip, GFS2_I(inode), name);
  608. if (error)
  609. goto fail_gunlock2;
  610. error = link_dinode(dip, name, GFS2_I(inode));
  611. if (error)
  612. goto fail_gunlock2;
  613. if (bh)
  614. brelse(bh);
  615. gfs2_trans_end(sdp);
  616. if (dip->i_alloc->al_rgd)
  617. gfs2_inplace_release(dip);
  618. gfs2_quota_unlock(dip);
  619. gfs2_alloc_put(dip);
  620. gfs2_glock_dq_uninit_m(2, ghs);
  621. mark_inode_dirty(inode);
  622. d_instantiate(dentry, inode);
  623. return 0;
  624. fail_gunlock2:
  625. gfs2_glock_dq_uninit(ghs + 1);
  626. if (inode && !IS_ERR(inode))
  627. iput(inode);
  628. fail_gunlock:
  629. gfs2_glock_dq_uninit(ghs);
  630. fail:
  631. if (bh)
  632. brelse(bh);
  633. return error;
  634. }
  635. /**
  636. * gfs2_create - Create a file
  637. * @dir: The directory in which to create the file
  638. * @dentry: The dentry of the new file
  639. * @mode: The mode of the new file
  640. *
  641. * Returns: errno
  642. */
  643. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  644. int mode, struct nameidata *nd)
  645. {
  646. struct inode *inode;
  647. int ret;
  648. for (;;) {
  649. ret = gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0);
  650. if (ret != -EEXIST || (nd && (nd->flags & LOOKUP_EXCL)))
  651. return ret;
  652. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  653. if (inode) {
  654. if (!IS_ERR(inode))
  655. break;
  656. return PTR_ERR(inode);
  657. }
  658. }
  659. d_instantiate(dentry, inode);
  660. return 0;
  661. }
  662. /**
  663. * gfs2_lookup - Look up a filename in a directory and return its inode
  664. * @dir: The directory inode
  665. * @dentry: The dentry of the new inode
  666. * @nd: passed from Linux VFS, ignored by us
  667. *
  668. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  669. *
  670. * Returns: errno
  671. */
  672. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  673. struct nameidata *nd)
  674. {
  675. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  676. if (inode && !IS_ERR(inode)) {
  677. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  678. struct gfs2_holder gh;
  679. int error;
  680. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  681. if (error) {
  682. iput(inode);
  683. return ERR_PTR(error);
  684. }
  685. gfs2_glock_dq_uninit(&gh);
  686. }
  687. return d_splice_alias(inode, dentry);
  688. }
  689. /**
  690. * gfs2_link - Link to a file
  691. * @old_dentry: The inode to link
  692. * @dir: Add link to this directory
  693. * @dentry: The name of the link
  694. *
  695. * Link the inode in "old_dentry" into the directory "dir" with the
  696. * name in "dentry".
  697. *
  698. * Returns: errno
  699. */
  700. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  701. struct dentry *dentry)
  702. {
  703. struct gfs2_inode *dip = GFS2_I(dir);
  704. struct gfs2_sbd *sdp = GFS2_SB(dir);
  705. struct inode *inode = old_dentry->d_inode;
  706. struct gfs2_inode *ip = GFS2_I(inode);
  707. struct gfs2_holder ghs[2];
  708. struct buffer_head *dibh;
  709. int alloc_required;
  710. int error;
  711. if (S_ISDIR(inode->i_mode))
  712. return -EPERM;
  713. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  714. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  715. error = gfs2_glock_nq(ghs); /* parent */
  716. if (error)
  717. goto out_parent;
  718. error = gfs2_glock_nq(ghs + 1); /* child */
  719. if (error)
  720. goto out_child;
  721. error = -ENOENT;
  722. if (inode->i_nlink == 0)
  723. goto out_gunlock;
  724. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  725. if (error)
  726. goto out_gunlock;
  727. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  728. switch (error) {
  729. case -ENOENT:
  730. break;
  731. case 0:
  732. error = -EEXIST;
  733. default:
  734. goto out_gunlock;
  735. }
  736. error = -EINVAL;
  737. if (!dip->i_inode.i_nlink)
  738. goto out_gunlock;
  739. error = -EFBIG;
  740. if (dip->i_entries == (u32)-1)
  741. goto out_gunlock;
  742. error = -EPERM;
  743. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  744. goto out_gunlock;
  745. error = -EINVAL;
  746. if (!ip->i_inode.i_nlink)
  747. goto out_gunlock;
  748. error = -EMLINK;
  749. if (ip->i_inode.i_nlink == (u32)-1)
  750. goto out_gunlock;
  751. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  752. if (error < 0)
  753. goto out_gunlock;
  754. error = 0;
  755. if (alloc_required) {
  756. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  757. if (!al) {
  758. error = -ENOMEM;
  759. goto out_gunlock;
  760. }
  761. error = gfs2_quota_lock_check(dip);
  762. if (error)
  763. goto out_alloc;
  764. al->al_requested = sdp->sd_max_dirres;
  765. error = gfs2_inplace_reserve(dip);
  766. if (error)
  767. goto out_gunlock_q;
  768. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  769. gfs2_rg_blocks(al) +
  770. 2 * RES_DINODE + RES_STATFS +
  771. RES_QUOTA, 0);
  772. if (error)
  773. goto out_ipres;
  774. } else {
  775. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  776. if (error)
  777. goto out_ipres;
  778. }
  779. error = gfs2_meta_inode_buffer(ip, &dibh);
  780. if (error)
  781. goto out_end_trans;
  782. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  783. if (error)
  784. goto out_brelse;
  785. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  786. inc_nlink(&ip->i_inode);
  787. ip->i_inode.i_ctime = CURRENT_TIME;
  788. gfs2_dinode_out(ip, dibh->b_data);
  789. mark_inode_dirty(&ip->i_inode);
  790. out_brelse:
  791. brelse(dibh);
  792. out_end_trans:
  793. gfs2_trans_end(sdp);
  794. out_ipres:
  795. if (alloc_required)
  796. gfs2_inplace_release(dip);
  797. out_gunlock_q:
  798. if (alloc_required)
  799. gfs2_quota_unlock(dip);
  800. out_alloc:
  801. if (alloc_required)
  802. gfs2_alloc_put(dip);
  803. out_gunlock:
  804. gfs2_glock_dq(ghs + 1);
  805. out_child:
  806. gfs2_glock_dq(ghs);
  807. out_parent:
  808. gfs2_holder_uninit(ghs);
  809. gfs2_holder_uninit(ghs + 1);
  810. if (!error) {
  811. ihold(inode);
  812. d_instantiate(dentry, inode);
  813. mark_inode_dirty(inode);
  814. }
  815. return error;
  816. }
  817. /*
  818. * gfs2_unlink_ok - check to see that a inode is still in a directory
  819. * @dip: the directory
  820. * @name: the name of the file
  821. * @ip: the inode
  822. *
  823. * Assumes that the lock on (at least) @dip is held.
  824. *
  825. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  826. */
  827. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  828. const struct gfs2_inode *ip)
  829. {
  830. int error;
  831. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  832. return -EPERM;
  833. if ((dip->i_inode.i_mode & S_ISVTX) &&
  834. dip->i_inode.i_uid != current_fsuid() &&
  835. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  836. return -EPERM;
  837. if (IS_APPEND(&dip->i_inode))
  838. return -EPERM;
  839. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  840. if (error)
  841. return error;
  842. error = gfs2_dir_check(&dip->i_inode, name, ip);
  843. if (error)
  844. return error;
  845. return 0;
  846. }
  847. /**
  848. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  849. * @dip: The parent directory
  850. * @name: The name of the entry in the parent directory
  851. * @bh: The inode buffer for the inode to be removed
  852. * @inode: The inode to be removed
  853. *
  854. * Called with all the locks and in a transaction. This will only be
  855. * called for a directory after it has been checked to ensure it is empty.
  856. *
  857. * Returns: 0 on success, or an error
  858. */
  859. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  860. const struct dentry *dentry,
  861. struct buffer_head *bh)
  862. {
  863. struct inode *inode = dentry->d_inode;
  864. struct gfs2_inode *ip = GFS2_I(inode);
  865. int error;
  866. error = gfs2_dir_del(dip, dentry);
  867. if (error)
  868. return error;
  869. ip->i_entries = 0;
  870. inode->i_ctime = CURRENT_TIME;
  871. if (S_ISDIR(inode->i_mode))
  872. clear_nlink(inode);
  873. else
  874. drop_nlink(inode);
  875. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  876. gfs2_dinode_out(ip, bh->b_data);
  877. mark_inode_dirty(inode);
  878. if (inode->i_nlink == 0)
  879. gfs2_unlink_di(inode);
  880. return 0;
  881. }
  882. /**
  883. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  884. * @dir: The inode of the directory containing the inode to unlink
  885. * @dentry: The file itself
  886. *
  887. * This routine uses the type of the inode as a flag to figure out
  888. * whether this is an unlink or an rmdir.
  889. *
  890. * Returns: errno
  891. */
  892. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  893. {
  894. struct gfs2_inode *dip = GFS2_I(dir);
  895. struct gfs2_sbd *sdp = GFS2_SB(dir);
  896. struct inode *inode = dentry->d_inode;
  897. struct gfs2_inode *ip = GFS2_I(inode);
  898. struct buffer_head *bh;
  899. struct gfs2_holder ghs[3];
  900. struct gfs2_rgrpd *rgd;
  901. struct gfs2_holder ri_gh;
  902. int error;
  903. error = gfs2_rindex_hold(sdp, &ri_gh);
  904. if (error)
  905. return error;
  906. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  907. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  908. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  909. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  910. error = gfs2_glock_nq(ghs); /* parent */
  911. if (error)
  912. goto out_parent;
  913. error = gfs2_glock_nq(ghs + 1); /* child */
  914. if (error)
  915. goto out_child;
  916. error = -ENOENT;
  917. if (inode->i_nlink == 0)
  918. goto out_rgrp;
  919. if (S_ISDIR(inode->i_mode)) {
  920. error = -ENOTEMPTY;
  921. if (ip->i_entries > 2 || inode->i_nlink > 2)
  922. goto out_rgrp;
  923. }
  924. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  925. if (error)
  926. goto out_rgrp;
  927. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  928. if (error)
  929. goto out_gunlock;
  930. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  931. if (error)
  932. goto out_gunlock;
  933. error = gfs2_meta_inode_buffer(ip, &bh);
  934. if (error)
  935. goto out_end_trans;
  936. error = gfs2_unlink_inode(dip, dentry, bh);
  937. brelse(bh);
  938. out_end_trans:
  939. gfs2_trans_end(sdp);
  940. out_gunlock:
  941. gfs2_glock_dq(ghs + 2);
  942. out_rgrp:
  943. gfs2_holder_uninit(ghs + 2);
  944. gfs2_glock_dq(ghs + 1);
  945. out_child:
  946. gfs2_holder_uninit(ghs + 1);
  947. gfs2_glock_dq(ghs);
  948. out_parent:
  949. gfs2_holder_uninit(ghs);
  950. gfs2_glock_dq_uninit(&ri_gh);
  951. return error;
  952. }
  953. /**
  954. * gfs2_symlink - Create a symlink
  955. * @dir: The directory to create the symlink in
  956. * @dentry: The dentry to put the symlink in
  957. * @symname: The thing which the link points to
  958. *
  959. * Returns: errno
  960. */
  961. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  962. const char *symname)
  963. {
  964. struct gfs2_sbd *sdp = GFS2_SB(dir);
  965. unsigned int size;
  966. size = strlen(symname);
  967. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  968. return -ENAMETOOLONG;
  969. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size);
  970. }
  971. /**
  972. * gfs2_mkdir - Make a directory
  973. * @dir: The parent directory of the new one
  974. * @dentry: The dentry of the new directory
  975. * @mode: The mode of the new directory
  976. *
  977. * Returns: errno
  978. */
  979. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  980. {
  981. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0);
  982. }
  983. /**
  984. * gfs2_mknod - Make a special file
  985. * @dir: The directory in which the special file will reside
  986. * @dentry: The dentry of the special file
  987. * @mode: The mode of the special file
  988. * @dev: The device specification of the special file
  989. *
  990. */
  991. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  992. dev_t dev)
  993. {
  994. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0);
  995. }
  996. /*
  997. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  998. * @this: move this
  999. * @to: to here
  1000. *
  1001. * Follow @to back to the root and make sure we don't encounter @this
  1002. * Assumes we already hold the rename lock.
  1003. *
  1004. * Returns: errno
  1005. */
  1006. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1007. {
  1008. struct inode *dir = &to->i_inode;
  1009. struct super_block *sb = dir->i_sb;
  1010. struct inode *tmp;
  1011. int error = 0;
  1012. igrab(dir);
  1013. for (;;) {
  1014. if (dir == &this->i_inode) {
  1015. error = -EINVAL;
  1016. break;
  1017. }
  1018. if (dir == sb->s_root->d_inode) {
  1019. error = 0;
  1020. break;
  1021. }
  1022. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1023. if (IS_ERR(tmp)) {
  1024. error = PTR_ERR(tmp);
  1025. break;
  1026. }
  1027. iput(dir);
  1028. dir = tmp;
  1029. }
  1030. iput(dir);
  1031. return error;
  1032. }
  1033. /**
  1034. * gfs2_rename - Rename a file
  1035. * @odir: Parent directory of old file name
  1036. * @odentry: The old dentry of the file
  1037. * @ndir: Parent directory of new file name
  1038. * @ndentry: The new dentry of the file
  1039. *
  1040. * Returns: errno
  1041. */
  1042. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1043. struct inode *ndir, struct dentry *ndentry)
  1044. {
  1045. struct gfs2_inode *odip = GFS2_I(odir);
  1046. struct gfs2_inode *ndip = GFS2_I(ndir);
  1047. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1048. struct gfs2_inode *nip = NULL;
  1049. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1050. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  1051. struct gfs2_rgrpd *nrgd;
  1052. unsigned int num_gh;
  1053. int dir_rename = 0;
  1054. int alloc_required = 0;
  1055. unsigned int x;
  1056. int error;
  1057. if (ndentry->d_inode) {
  1058. nip = GFS2_I(ndentry->d_inode);
  1059. if (ip == nip)
  1060. return 0;
  1061. }
  1062. error = gfs2_rindex_hold(sdp, &ri_gh);
  1063. if (error)
  1064. return error;
  1065. if (odip != ndip) {
  1066. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1067. 0, &r_gh);
  1068. if (error)
  1069. goto out;
  1070. if (S_ISDIR(ip->i_inode.i_mode)) {
  1071. dir_rename = 1;
  1072. /* don't move a dirctory into it's subdir */
  1073. error = gfs2_ok_to_move(ip, ndip);
  1074. if (error)
  1075. goto out_gunlock_r;
  1076. }
  1077. }
  1078. num_gh = 1;
  1079. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1080. if (odip != ndip) {
  1081. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1082. num_gh++;
  1083. }
  1084. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1085. num_gh++;
  1086. if (nip) {
  1087. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1088. num_gh++;
  1089. /* grab the resource lock for unlink flag twiddling
  1090. * this is the case of the target file already existing
  1091. * so we unlink before doing the rename
  1092. */
  1093. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  1094. if (nrgd)
  1095. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1096. }
  1097. for (x = 0; x < num_gh; x++) {
  1098. error = gfs2_glock_nq(ghs + x);
  1099. if (error)
  1100. goto out_gunlock;
  1101. }
  1102. error = -ENOENT;
  1103. if (ip->i_inode.i_nlink == 0)
  1104. goto out_gunlock;
  1105. /* Check out the old directory */
  1106. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1107. if (error)
  1108. goto out_gunlock;
  1109. /* Check out the new directory */
  1110. if (nip) {
  1111. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1112. if (error)
  1113. goto out_gunlock;
  1114. if (nip->i_inode.i_nlink == 0) {
  1115. error = -EAGAIN;
  1116. goto out_gunlock;
  1117. }
  1118. if (S_ISDIR(nip->i_inode.i_mode)) {
  1119. if (nip->i_entries < 2) {
  1120. gfs2_consist_inode(nip);
  1121. error = -EIO;
  1122. goto out_gunlock;
  1123. }
  1124. if (nip->i_entries > 2) {
  1125. error = -ENOTEMPTY;
  1126. goto out_gunlock;
  1127. }
  1128. }
  1129. } else {
  1130. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1131. if (error)
  1132. goto out_gunlock;
  1133. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1134. switch (error) {
  1135. case -ENOENT:
  1136. error = 0;
  1137. break;
  1138. case 0:
  1139. error = -EEXIST;
  1140. default:
  1141. goto out_gunlock;
  1142. };
  1143. if (odip != ndip) {
  1144. if (!ndip->i_inode.i_nlink) {
  1145. error = -ENOENT;
  1146. goto out_gunlock;
  1147. }
  1148. if (ndip->i_entries == (u32)-1) {
  1149. error = -EFBIG;
  1150. goto out_gunlock;
  1151. }
  1152. if (S_ISDIR(ip->i_inode.i_mode) &&
  1153. ndip->i_inode.i_nlink == (u32)-1) {
  1154. error = -EMLINK;
  1155. goto out_gunlock;
  1156. }
  1157. }
  1158. }
  1159. /* Check out the dir to be renamed */
  1160. if (dir_rename) {
  1161. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1162. if (error)
  1163. goto out_gunlock;
  1164. }
  1165. if (nip == NULL)
  1166. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1167. error = alloc_required;
  1168. if (error < 0)
  1169. goto out_gunlock;
  1170. error = 0;
  1171. if (alloc_required) {
  1172. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  1173. if (!al) {
  1174. error = -ENOMEM;
  1175. goto out_gunlock;
  1176. }
  1177. error = gfs2_quota_lock_check(ndip);
  1178. if (error)
  1179. goto out_alloc;
  1180. al->al_requested = sdp->sd_max_dirres;
  1181. error = gfs2_inplace_reserve_ri(ndip);
  1182. if (error)
  1183. goto out_gunlock_q;
  1184. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1185. gfs2_rg_blocks(al) +
  1186. 4 * RES_DINODE + 4 * RES_LEAF +
  1187. RES_STATFS + RES_QUOTA + 4, 0);
  1188. if (error)
  1189. goto out_ipreserv;
  1190. } else {
  1191. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1192. 5 * RES_LEAF + 4, 0);
  1193. if (error)
  1194. goto out_gunlock;
  1195. }
  1196. /* Remove the target file, if it exists */
  1197. if (nip) {
  1198. struct buffer_head *bh;
  1199. error = gfs2_meta_inode_buffer(nip, &bh);
  1200. if (error)
  1201. goto out_end_trans;
  1202. error = gfs2_unlink_inode(ndip, ndentry, bh);
  1203. brelse(bh);
  1204. }
  1205. if (dir_rename) {
  1206. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1207. if (error)
  1208. goto out_end_trans;
  1209. } else {
  1210. struct buffer_head *dibh;
  1211. error = gfs2_meta_inode_buffer(ip, &dibh);
  1212. if (error)
  1213. goto out_end_trans;
  1214. ip->i_inode.i_ctime = CURRENT_TIME;
  1215. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1216. gfs2_dinode_out(ip, dibh->b_data);
  1217. brelse(dibh);
  1218. }
  1219. error = gfs2_dir_del(odip, odentry);
  1220. if (error)
  1221. goto out_end_trans;
  1222. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1223. if (error)
  1224. goto out_end_trans;
  1225. out_end_trans:
  1226. gfs2_trans_end(sdp);
  1227. out_ipreserv:
  1228. if (alloc_required)
  1229. gfs2_inplace_release(ndip);
  1230. out_gunlock_q:
  1231. if (alloc_required)
  1232. gfs2_quota_unlock(ndip);
  1233. out_alloc:
  1234. if (alloc_required)
  1235. gfs2_alloc_put(ndip);
  1236. out_gunlock:
  1237. while (x--) {
  1238. gfs2_glock_dq(ghs + x);
  1239. gfs2_holder_uninit(ghs + x);
  1240. }
  1241. out_gunlock_r:
  1242. if (r_gh.gh_gl)
  1243. gfs2_glock_dq_uninit(&r_gh);
  1244. out:
  1245. gfs2_glock_dq_uninit(&ri_gh);
  1246. return error;
  1247. }
  1248. /**
  1249. * gfs2_follow_link - Follow a symbolic link
  1250. * @dentry: The dentry of the link
  1251. * @nd: Data that we pass to vfs_follow_link()
  1252. *
  1253. * This can handle symlinks of any size.
  1254. *
  1255. * Returns: 0 on success or error code
  1256. */
  1257. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1258. {
  1259. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1260. struct gfs2_holder i_gh;
  1261. struct buffer_head *dibh;
  1262. unsigned int size;
  1263. char *buf;
  1264. int error;
  1265. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1266. error = gfs2_glock_nq(&i_gh);
  1267. if (error) {
  1268. gfs2_holder_uninit(&i_gh);
  1269. nd_set_link(nd, ERR_PTR(error));
  1270. return NULL;
  1271. }
  1272. size = (unsigned int)i_size_read(&ip->i_inode);
  1273. if (size == 0) {
  1274. gfs2_consist_inode(ip);
  1275. buf = ERR_PTR(-EIO);
  1276. goto out;
  1277. }
  1278. error = gfs2_meta_inode_buffer(ip, &dibh);
  1279. if (error) {
  1280. buf = ERR_PTR(error);
  1281. goto out;
  1282. }
  1283. buf = kzalloc(size + 1, GFP_NOFS);
  1284. if (!buf)
  1285. buf = ERR_PTR(-ENOMEM);
  1286. else
  1287. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1288. brelse(dibh);
  1289. out:
  1290. gfs2_glock_dq_uninit(&i_gh);
  1291. nd_set_link(nd, buf);
  1292. return NULL;
  1293. }
  1294. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1295. {
  1296. char *s = nd_get_link(nd);
  1297. if (!IS_ERR(s))
  1298. kfree(s);
  1299. }
  1300. /**
  1301. * gfs2_permission -
  1302. * @inode: The inode
  1303. * @mask: The mask to be tested
  1304. * @flags: Indicates whether this is an RCU path walk or not
  1305. *
  1306. * This may be called from the VFS directly, or from within GFS2 with the
  1307. * inode locked, so we look to see if the glock is already locked and only
  1308. * lock the glock if its not already been done.
  1309. *
  1310. * Returns: errno
  1311. */
  1312. int gfs2_permission(struct inode *inode, int mask)
  1313. {
  1314. struct gfs2_inode *ip;
  1315. struct gfs2_holder i_gh;
  1316. int error;
  1317. int unlock = 0;
  1318. ip = GFS2_I(inode);
  1319. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1320. if (mask & MAY_NOT_BLOCK)
  1321. return -ECHILD;
  1322. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1323. if (error)
  1324. return error;
  1325. unlock = 1;
  1326. }
  1327. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1328. error = -EACCES;
  1329. else
  1330. error = generic_permission(inode, mask);
  1331. if (unlock)
  1332. gfs2_glock_dq_uninit(&i_gh);
  1333. return error;
  1334. }
  1335. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1336. {
  1337. struct inode *inode = &ip->i_inode;
  1338. struct buffer_head *dibh;
  1339. int error;
  1340. error = gfs2_meta_inode_buffer(ip, &dibh);
  1341. if (error)
  1342. return error;
  1343. setattr_copy(inode, attr);
  1344. mark_inode_dirty(inode);
  1345. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1346. gfs2_dinode_out(ip, dibh->b_data);
  1347. brelse(dibh);
  1348. return 0;
  1349. }
  1350. /**
  1351. * gfs2_setattr_simple -
  1352. * @ip:
  1353. * @attr:
  1354. *
  1355. * Returns: errno
  1356. */
  1357. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1358. {
  1359. int error;
  1360. if (current->journal_info)
  1361. return __gfs2_setattr_simple(ip, attr);
  1362. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1363. if (error)
  1364. return error;
  1365. error = __gfs2_setattr_simple(ip, attr);
  1366. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1367. return error;
  1368. }
  1369. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1370. {
  1371. struct gfs2_inode *ip = GFS2_I(inode);
  1372. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1373. u32 ouid, ogid, nuid, ngid;
  1374. int error;
  1375. ouid = inode->i_uid;
  1376. ogid = inode->i_gid;
  1377. nuid = attr->ia_uid;
  1378. ngid = attr->ia_gid;
  1379. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1380. ouid = nuid = NO_QUOTA_CHANGE;
  1381. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1382. ogid = ngid = NO_QUOTA_CHANGE;
  1383. if (!gfs2_alloc_get(ip))
  1384. return -ENOMEM;
  1385. error = gfs2_quota_lock(ip, nuid, ngid);
  1386. if (error)
  1387. goto out_alloc;
  1388. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1389. error = gfs2_quota_check(ip, nuid, ngid);
  1390. if (error)
  1391. goto out_gunlock_q;
  1392. }
  1393. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1394. if (error)
  1395. goto out_gunlock_q;
  1396. error = gfs2_setattr_simple(ip, attr);
  1397. if (error)
  1398. goto out_end_trans;
  1399. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1400. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1401. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1402. gfs2_quota_change(ip, blocks, nuid, ngid);
  1403. }
  1404. out_end_trans:
  1405. gfs2_trans_end(sdp);
  1406. out_gunlock_q:
  1407. gfs2_quota_unlock(ip);
  1408. out_alloc:
  1409. gfs2_alloc_put(ip);
  1410. return error;
  1411. }
  1412. /**
  1413. * gfs2_setattr - Change attributes on an inode
  1414. * @dentry: The dentry which is changing
  1415. * @attr: The structure describing the change
  1416. *
  1417. * The VFS layer wants to change one or more of an inodes attributes. Write
  1418. * that change out to disk.
  1419. *
  1420. * Returns: errno
  1421. */
  1422. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1423. {
  1424. struct inode *inode = dentry->d_inode;
  1425. struct gfs2_inode *ip = GFS2_I(inode);
  1426. struct gfs2_holder i_gh;
  1427. int error;
  1428. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1429. if (error)
  1430. return error;
  1431. error = -EPERM;
  1432. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1433. goto out;
  1434. error = inode_change_ok(inode, attr);
  1435. if (error)
  1436. goto out;
  1437. if (attr->ia_valid & ATTR_SIZE)
  1438. error = gfs2_setattr_size(inode, attr->ia_size);
  1439. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1440. error = setattr_chown(inode, attr);
  1441. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1442. error = gfs2_acl_chmod(ip, attr);
  1443. else
  1444. error = gfs2_setattr_simple(ip, attr);
  1445. out:
  1446. gfs2_glock_dq_uninit(&i_gh);
  1447. if (!error)
  1448. mark_inode_dirty(inode);
  1449. return error;
  1450. }
  1451. /**
  1452. * gfs2_getattr - Read out an inode's attributes
  1453. * @mnt: The vfsmount the inode is being accessed from
  1454. * @dentry: The dentry to stat
  1455. * @stat: The inode's stats
  1456. *
  1457. * This may be called from the VFS directly, or from within GFS2 with the
  1458. * inode locked, so we look to see if the glock is already locked and only
  1459. * lock the glock if its not already been done. Note that its the NFS
  1460. * readdirplus operation which causes this to be called (from filldir)
  1461. * with the glock already held.
  1462. *
  1463. * Returns: errno
  1464. */
  1465. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1466. struct kstat *stat)
  1467. {
  1468. struct inode *inode = dentry->d_inode;
  1469. struct gfs2_inode *ip = GFS2_I(inode);
  1470. struct gfs2_holder gh;
  1471. int error;
  1472. int unlock = 0;
  1473. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1474. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1475. if (error)
  1476. return error;
  1477. unlock = 1;
  1478. }
  1479. generic_fillattr(inode, stat);
  1480. if (unlock)
  1481. gfs2_glock_dq_uninit(&gh);
  1482. return 0;
  1483. }
  1484. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1485. const void *data, size_t size, int flags)
  1486. {
  1487. struct inode *inode = dentry->d_inode;
  1488. struct gfs2_inode *ip = GFS2_I(inode);
  1489. struct gfs2_holder gh;
  1490. int ret;
  1491. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1492. ret = gfs2_glock_nq(&gh);
  1493. if (ret == 0) {
  1494. ret = generic_setxattr(dentry, name, data, size, flags);
  1495. gfs2_glock_dq(&gh);
  1496. }
  1497. gfs2_holder_uninit(&gh);
  1498. return ret;
  1499. }
  1500. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1501. void *data, size_t size)
  1502. {
  1503. struct inode *inode = dentry->d_inode;
  1504. struct gfs2_inode *ip = GFS2_I(inode);
  1505. struct gfs2_holder gh;
  1506. int ret;
  1507. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1508. ret = gfs2_glock_nq(&gh);
  1509. if (ret == 0) {
  1510. ret = generic_getxattr(dentry, name, data, size);
  1511. gfs2_glock_dq(&gh);
  1512. }
  1513. gfs2_holder_uninit(&gh);
  1514. return ret;
  1515. }
  1516. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1517. {
  1518. struct inode *inode = dentry->d_inode;
  1519. struct gfs2_inode *ip = GFS2_I(inode);
  1520. struct gfs2_holder gh;
  1521. int ret;
  1522. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1523. ret = gfs2_glock_nq(&gh);
  1524. if (ret == 0) {
  1525. ret = generic_removexattr(dentry, name);
  1526. gfs2_glock_dq(&gh);
  1527. }
  1528. gfs2_holder_uninit(&gh);
  1529. return ret;
  1530. }
  1531. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1532. u64 start, u64 len)
  1533. {
  1534. struct gfs2_inode *ip = GFS2_I(inode);
  1535. struct gfs2_holder gh;
  1536. int ret;
  1537. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1538. if (ret)
  1539. return ret;
  1540. mutex_lock(&inode->i_mutex);
  1541. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1542. if (ret)
  1543. goto out;
  1544. if (gfs2_is_stuffed(ip)) {
  1545. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1546. u64 size = i_size_read(inode);
  1547. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1548. FIEMAP_EXTENT_DATA_INLINE;
  1549. phys += sizeof(struct gfs2_dinode);
  1550. phys += start;
  1551. if (start + len > size)
  1552. len = size - start;
  1553. if (start < size)
  1554. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1555. len, flags);
  1556. if (ret == 1)
  1557. ret = 0;
  1558. } else {
  1559. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1560. gfs2_block_map);
  1561. }
  1562. gfs2_glock_dq_uninit(&gh);
  1563. out:
  1564. mutex_unlock(&inode->i_mutex);
  1565. return ret;
  1566. }
  1567. const struct inode_operations gfs2_file_iops = {
  1568. .permission = gfs2_permission,
  1569. .setattr = gfs2_setattr,
  1570. .getattr = gfs2_getattr,
  1571. .setxattr = gfs2_setxattr,
  1572. .getxattr = gfs2_getxattr,
  1573. .listxattr = gfs2_listxattr,
  1574. .removexattr = gfs2_removexattr,
  1575. .fiemap = gfs2_fiemap,
  1576. .get_acl = gfs2_get_acl,
  1577. };
  1578. const struct inode_operations gfs2_dir_iops = {
  1579. .create = gfs2_create,
  1580. .lookup = gfs2_lookup,
  1581. .link = gfs2_link,
  1582. .unlink = gfs2_unlink,
  1583. .symlink = gfs2_symlink,
  1584. .mkdir = gfs2_mkdir,
  1585. .rmdir = gfs2_unlink,
  1586. .mknod = gfs2_mknod,
  1587. .rename = gfs2_rename,
  1588. .permission = gfs2_permission,
  1589. .setattr = gfs2_setattr,
  1590. .getattr = gfs2_getattr,
  1591. .setxattr = gfs2_setxattr,
  1592. .getxattr = gfs2_getxattr,
  1593. .listxattr = gfs2_listxattr,
  1594. .removexattr = gfs2_removexattr,
  1595. .fiemap = gfs2_fiemap,
  1596. .get_acl = gfs2_get_acl,
  1597. };
  1598. const struct inode_operations gfs2_symlink_iops = {
  1599. .readlink = generic_readlink,
  1600. .follow_link = gfs2_follow_link,
  1601. .put_link = gfs2_put_link,
  1602. .permission = gfs2_permission,
  1603. .setattr = gfs2_setattr,
  1604. .getattr = gfs2_getattr,
  1605. .setxattr = gfs2_setxattr,
  1606. .getxattr = gfs2_getxattr,
  1607. .listxattr = gfs2_listxattr,
  1608. .removexattr = gfs2_removexattr,
  1609. .fiemap = gfs2_fiemap,
  1610. .get_acl = gfs2_get_acl,
  1611. };